[Tutor] how come this doesnt work
Alex Hall
mehgcap at gmail.com
Sat Jan 15 03:40:47 CET 2011
On 1/14/11, walter weston <hacker0100 at hotmail.com> wrote:
>
> I generate a random number(supposedly a password, in my case its just a long
> floating point lol),I want the user to reinput that number and I want to
> print a string if the number entered is correct. so if m==num(num is the
> number generated and m is the variable which stores the input ) then I want
> to print 'you entered correctly, proceed'.
>
> here is my code..
>
> import random
> for x in range(0,1):
Note that you could just say: for x in range(1) since range() starts
at 0 automatically. Note, too, that this will run once, so you really
do not need a for loop at all.
> num = random.random()
> print (num)
> m=input('input pass:')
"m" is currently a string since it is what is returned by input. Your
num, though, is an integer...
> if m==num:
> print('you entered correctly, proceed')
I assume the problem is that it never prints? If not, this is because
m is a string while num is an integer (int for short). Add:
m=int(m)
just before the if statement. This causes m to turn from a string into
an integer and is what is known as "casting" or "type casting", if I
have my vocabulary correct. Essentially, while you see a number in
both m and num, Python sees two very different binary values and so
they are never equal.
Here goes a very odd example, but it is all I can come up with at the
moment. Imagine two boxes, each the same size and each the same color.
If you weigh them, though, you find one to be much heavier than the
other; they look the same, but they are not. Casting is like adding
weight to the lighter box until it equals the weight of the heavier
box; now they *are* equal, with the same dimensions, color, and
weight. I told you it would not be a great example.
HTH.
>
> It's very noobish dont give me complex replys, and dont be to rude or laugh
> at me, as I am a beginner In the programming domain.
>
--
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap
More information about the Tutor
mailing list