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

  • Context: Python 
  • Thread starter Thread starter OrbitalPower
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around a Python programming issue related to input validation for a five-digit number. Participants explore various approaches to ensure that the input meets the specified criteria, comparing Python's behavior with that of Java.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their initial Python code and expresses confusion about why it prompts for input again after an invalid entry.
  • Another participant notes that the Python code throws an exception when attempting to divide a non-numeric input, such as the string "dang".
  • Some participants suggest alternative ways to structure the while loop for input validation, including different logical conditions.
  • There is a mention of Java's input handling, with a participant speculating that it might return 0 for invalid input, contrasting it with Python's behavior.
  • One participant points out that the condition "not(x < 10000 and x > 99999)" is logically flawed, as no number can satisfy both conditions simultaneously.
  • A later reply acknowledges a mistake in the logical conditions used in the Python code and suggests that reversing the signs may resolve the issue.
  • Another participant introduces a more concise way to express the input validation condition using Python's syntactic sugar for intervals.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of various logical conditions for input validation, and there is no consensus on the best approach. Some participants agree on the flaws in the original logic, while others explore alternative methods without reaching a definitive solution.

Contextual Notes

Some participants note the potential for confusion due to the handling of non-numeric input in Python, which can lead to exceptions, while others suggest debugging strategies to clarify the behavior of the code.

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
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'?)
 
"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..
 
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.
 
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"
 
"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.
 
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.
 
Incidentally, python has some syntactic sugar for intervals: your test could be

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

Similar threads

Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
3
Views
3K