Tkinter GUI Error

Chris Angelico rosuav at gmail.com
Mon Jan 13 22:12:01 EST 2014


On Tue, Jan 14, 2014 at 5:49 AM,  <fluttershy363 at gmail.com> wrote:
>     entry = entry1var.get()
>     if entry == num1:
>     elif entry > num1:
>     elif entry < num1:
>
> num1 =str(random.randint(10,99))
> num2 =str(random.randint(10,99))
> num3 =str(random.randint(10,99))
> mastercode = num1+num2+num3

Be careful of code like this. You've specified that your three parts
range from 10 through 99, so this will work as long as the user knows
this and enters exactly two digits. Doing inequality comparisons on
strings that represent numbers will work as long as they're the same
length, but if the lengths vary, the string comparisons will start at
the beginning - not what most people will expect. These are all true:

"2" > "10"
"3.14159" > "2,000,000"
"42" < "Life, the universe, and everything"
"00012" < "12"

If your intention is to have a six-digit number, you could simply ask
for one, and then format the pieces accordingly:

num = random.randint(1,999999)
num_str = "%06d" % num

You can then slice up num_str as needed (it'll have leading zeroes if
it needs them), or you can do numerical comparisons against num
itself.

ChrisA



More information about the Python-list mailing list