Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python woes

  1. Aug 1, 2008 #1
    python woes:

    Code (Text):
    ~/code/python$ cat five.py
    #!/usr/bin/env python

    x = input('Enter a five digit number: ')
    while (not(x < 10000 and x > 99999)):
            x = input('Five digits please: ')

    firstDigit  =  x / 10000
    secondDigit = (x / 1000) % 10
    thirdDigit  = (x / 100)  % 10
    fourthDigit = (x / 10)   % 10
    fifthDigit  =  x         % 10

    print "The digit a is", firstDigit, secondDigit, thirdDigit, fourthDigit, fifthDigit
    (1)~/code/python$ ./five.py
    Enter a five digit number: 54
    Five digits please: 54321
    Five digits please: dang
    Traceback (most recent call last):
      File "./five.py", line 5, in ?
        x = input('Five digits please: ')
      File "<string>", line 0, in ?

    I don't understand why it asks for five digits again.

    My original code was this:

    Code (Text):
    x = input('Enter a five digit number: ')
    while (x <= 9999 or x >= 99999):
            x = input('Five digits please: ')
    The statements should have been equivalent but it causes problems when I do it the first way.

    In java:

    x = input.nextInt();
    while (!(x > 10000 && x < 99999))
    {
    System.out.println("Five digits retard: ");
    x = input.nextInt();
    }

    It works like it's supposed to i believe.
     
    Last edited: Aug 1, 2008
  2. jcsd
  3. Aug 1, 2008 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    The python code throws an exception when you try and divide the work 'dang' by 10000

    The java code "input.nextInt()" makes the input an int (I don't know java, presumably it returns 0 for 'dang'?)
     
  4. Aug 1, 2008 #3
    I don't know python, but have you tried others way like

    e.g.
    x = 0
    while (not(x < 10000 and x > 99999)):
    x = input('Five digits please: ')

    or put while in different way:
    while (not(x < 10000)):
    while (not(x > 99999)):

    Usually when the code doesn't work I try to use different ways..
     
  5. Aug 1, 2008 #4
    I typed "dang" because i believe it should have exited the while loop at that point.


    Another equivalent statement would be not(x>10000) or not(x < 99999).

    I will try them, but really I can already can get it to work fine, I just wondered why it'd stay in the loop on seemingly equivalent statements.
     
  6. Aug 1, 2008 #5
    one way is to debug..
    Code (Text):

    x = 50000; // try 10000, 99999, 9999, upperlimit+1 ..
    //Print x, boolean, ...
    while (not(x < 10000 and x > 99999)):
            PRINT "I am in loop"

    PRINT "I am out"
     
     
  7. Aug 2, 2008 #6
    "not(x < 10000 and x > 99999)"

    Think about this for a moment. When will a number ever be both less than 10000 and greater than 99999? Never! So this statement is always true.
     
  8. Aug 2, 2008 #7
    Yes, I wrote the signs the wrong way in the python version for some reason and wasn't paying attention. I'm sure it'll work when I reverse them.
     
  9. Aug 2, 2008 #8

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Incidentally, python has some syntactic sugar for intervals: your test could be

    while not (10000 <= x <= 99999):
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Python woes
  1. Error on Python (Replies: 9)

  2. Chess with Python (Replies: 2)

  3. Is Python the future? (Replies: 10)

  4. Python installation (Replies: 10)

Loading...