Closed Thread
Results 1 to 6 of 6

Thread: Testing byte size?

  1. #1
    Tsarj's Avatar
    Tsarj is offline Learning Programmer
    Join Date
    Nov 2007
    Location
    Phoenix, Arizona
    Posts
    36
    Rep Power
    0

    Testing byte size?

    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):

    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.')
    Any particular reason this is happening??

    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?
    AFK, [War]

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Testing byte size?

    This is what I am getting for the numbers;
    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.
    I also tested 2, 19 and 2, 10;
    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.
    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.
    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!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,489
    Blog Entries
    75
    Rep Power
    143

    Re: Testing byte size?

    Are they being compared as strings or as numbers? The result makes perfect sense from a lexicographic ordering: "10" < "2"
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: Testing byte size?

    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: '))

  6. #5
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Testing byte size?

    How is not working for 19 and 2 then but for everything else?

    Makes sense if the variables are Strings however.

  7. #6
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: Testing byte size?

    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
    Code:
    >>> n = input("enter a number: ")
    enter a number: 19
    >>> type(n)
    <type 'int'>
    From python 3.1
    Code:
    >>> n = input("enter a number: ")
    enter a number: 19
    >>> type(n)
    <class 'str'>
    So if you are using python 3.0 or later then you have to specifically cast the users input to an integer.

    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.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. J2EE: Limit File Size or Request Size
    By tarek.mostafa in forum Java Help
    Replies: 0
    Last Post: 05-27-2010, 05:46 AM
  2. Byte Adder
    By iluxon4ik in forum Visual Basic Programming
    Replies: 3
    Last Post: 02-02-2010, 03:43 AM
  3. Byte Switcher
    By Guest in forum Classes and Code Snippets
    Replies: 0
    Last Post: 09-27-2009, 03:19 AM
  4. Change font size of 'Product', 'Size', ...
    By DavidBoggitt in forum ionFiles
    Replies: 1
    Last Post: 09-03-2009, 06:21 PM
  5. Size/Size on Disk
    By BlaineSch in forum Technology Ramble
    Replies: 14
    Last Post: 07-03-2009, 05:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts