Hello Fellas,
I have recently been learning Python and I have a very strange bug. It's a very, very simple program: input a number, store it in a variable, input another number, store in another variable, then check which is greater, and output what number is maximum and which is minimum.
Now, the program works great with most numbers. But when you compare numbers 2~9 to 10~19, it's completely wrong. The lesser number is always printed as greater... O_o
Here is the code (excuse me if there is a better way to do it. And if so, please point it out):
Any particular reason this is happening??Code:#!/usr/bin/python3.0 while True: first = (input('Enter a number: ')) if first == 'quit': break second = (input('Enter another number: ')) if second == 'quit': break elif first == second: print(first, 'is equal to', second, '.') elif first < second: print(second, 'is maximum.') print(first, 'is minimum.') else: print(first, 'is maximum.') print(second, 'is minimum.') print('Done.')
EDIT -- I have tested this code using the Python 2.6 Interpreter and the code works just fine. Is there a reason why the Python 3.0 Interpreter is causing this bug?
This is what I am getting for the numbers;
I also tested 2, 19 and 2, 10;Code:Enter a number: 19 Enter another number: 2 2 is maximum. 19 is minimum. Enter a number: 10 Enter another number: 2 2 is maximum. 10 is minimum.
It might be a bug in Python because that makes no sense at all to why it is doing that. I know Python 3.1 is out so I tested it in that as well which it gave the above errors... I see no clue in the world to why it is doing this, as you mentioned it must be the Interpreter.Code:Enter a number: 2 Enter another number: 19 2 is maximum. 19 is minimum. Enter a number: 2 Enter another number: 10 2 is maximum. 10 is minimum.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!
Are they being compared as strings or as numbers? The result makes perfect sense from a lexicographic ordering: "10" < "2"
I'm pretty sure WP is right here. As far as I can remember the input() method returns a string. You need to cast the user input as an int, so your input lines should read
Code:first = int(input('Enter a number: ')) second = int(input('Enter a number: '))
How is not working for 19 and 2 then but for everything else?
Makes sense if the variables are Strings however.
I've just checked and the reason it works on python 2.6 but not 3.0 is because of a change to input()
From python 2.6
From python 3.1Code:>>> n = input("enter a number: ") enter a number: 19 >>> type(n) <type 'int'>
So if you are using python 3.0 or later then you have to specifically cast the users input to an integer.Code:>>> n = input("enter a number: ") enter a number: 19 >>> type(n) <class 'str'>
I must admit that I wasn't aware that input() behaved differently under 2.6. I always use int(input()) anyway. Probably not a bad idea if you don't know what version of python your user will have access to.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks