[Tutor] Random number selection
Alan Gauld
alan.gauld at btinternet.com
Tue Jan 3 12:44:11 CET 2012
On 03/01/12 01:52, shane wrote:
> I was wondering is there a way to have a variable generate a random
> integer each time the variable is called.
You can call a function to assign a value to a variable,
but you can't "call" a variable (unless its assigned to
a function but that's another subject entirely!).
> Ive tried random.randint(a, b) and the range one to.
> It selects a random number and assigns it to the variable
Yes, that's what it's supposed to do. So where do you have a problem?
> this part of the program would be math equations
> with random number selections
> x=ran y=ran z=x*y
In Python that translates to
x = random.randint(a,b)
y = random.randint(a,b)
z = x*y
> x * y = z
But this doesn't make sense unless you mean an equality test. In which
case in Python it's written:
x*y == z
and given the code above should equate to True
> s=input=('x*y= ')
Too many equal signs there. I assume you mean
s = input("x*y= ")
Which will assign a string value from the user to s(*). I'm not sure
what you think the string will contain? The letter 'z' maybe? or some
arbitrary number that you need to factorise to come up with new x,y
values? I can only guess. I'll assume the second option - a string
version of a number
(*) I'm assuming you are using Python version 3? If not input
could return a number rather than a string but since input()
is not secure we generally recommend you don;pt use it in
version 2.
> if s != z
z is a number, s a string so you need to convert:
if int(s) != z:
> There are many things I need to solve its my first program.
True, but at the moment I don't think we understand what
it is you are trying to do.
> But I just need help on assigning a random number and looping through to
> get diff values for x and y
A random number or multiple random numbers?
And do you want the random numbers assigned to x,y or something else?
And "looping through" what?
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list