Solve Python Woes: How to Get a Five Digit Number Input in One Try

  • Thread starter OrbitalPower
  • Start date
  • Tags
    Python
This is equivalent to your test, but I find it more readable. In summary, the conversation is about a person's difficulties with a python code that asks for a five digit number and repeats the prompt until a five digit number is entered. The person discusses their code and the equivalent statements that should produce the same result but cause problems. It is suggested to try different ways and debug the code. The conversation also touches on the differences between python and java. Finally, a more readable version of the code is suggested using python's syntactic sugar for intervals.
  • #1
OrbitalPower
python woes:

Code:
~/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:
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 by a moderator:
Technology news on Phys.org
  • #2
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'?)
 
  • #3
"The statements should have been equivalent but it causes problems when I do it the first way."

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..
 
  • #4
mgb_phys said:
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'?)

I typed "dang" because i believe it should have exited the while loop at that point.

rootX said:
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..

rootX said:
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..


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.
 
  • #5
OrbitalPower said:
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.

one way is to debug..
Code:
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"
 
  • #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.
 
  • #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.
 
  • #8
Incidentally, python has some syntactic sugar for intervals: your test could be

while not (10000 <= x <= 99999):
 

Related to Solve Python Woes: How to Get a Five Digit Number Input in One Try

1. How can I get a five digit number input in one try using Python?

To get a five digit number input in one try using Python, you can use the input() function and specify the data type as an integer, along with the prompt message. For example, you can use the code: number = int(input("Enter a five digit number: ")). This will prompt the user to enter a number and convert it to an integer, and if the input is not a valid integer, an error will be displayed.

2. What happens if the user enters a number with less or more than five digits?

If the user enters a number with less or more than five digits, the code will still run but it will not be considered a valid five digit number. To ensure that the user enters a five digit number, you can add a validation check in your code using conditional statements, such as if len(str(number)) != 5:, which will only allow the code to run if the input is exactly five digits long.

3. Can I use the input() function to get a five digit number with decimals?

Yes, you can use the input() function to get a five digit number with decimals. However, you will need to specify the data type as a float instead of an integer. For example, you can use the code: number = float(input("Enter a five digit number with decimals: ")). Keep in mind that the input will still need to be exactly five digits long, including the decimals.

4. Is there a more efficient way to get a five digit number input in one try?

Yes, there are other ways to get a five digit number input in one try using Python. One way is to use the raw_input() function, which will take the user's input as a string instead of converting it to an integer. You can then use string methods to check if the input is exactly five digits long. Another way is to use the split() method to separate the input into individual digits and check the length of the resulting list. However, using the input() function with data type specified is the simplest and most direct way.

5. Can I use this method to get inputs for other data types like strings or lists?

No, this method is specifically for getting a five digit number input in one try using Python. To get inputs for other data types, you will need to specify the data type accordingly. For example, to get a string input, you can use the code: string = input("Enter a string: "). To get a list input, you can use the code: list = input("Enter a list: ").split(), which will split the input into individual elements and store them in a list.

Similar threads

  • Programming and Computer Science
Replies
3
Views
923
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
6K
  • Programming and Computer Science
Replies
21
Views
8K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top