10

I have this code in python 3,to check for errors on input but i need to determine that the input is an integer and if not to print the error message.

can anyone help me to figure out the code in my while loop. Thanks

price = 110;

ttt = 1;

while price < 0 or price > 100:

    price = input('Please enter your marks for Maths:');
    ttt =ttt +1;
    if ttt >= 2:
        print( 'This is an invalid entry, Please enter a number between 0 and 100')
6
  • 1
    Use raw_input, not input. Commented Dec 27, 2012 at 22:47
  • Maybe this is what you're after: stackoverflow.com/questions/354038/… Commented Dec 27, 2012 at 22:49
  • 1
    Also if ttt >= 2 will always evaluate to true the first time in the loop, is this really the behaviour you want? Commented Dec 27, 2012 at 22:49
  • @minitech OP might be using Python 3 Commented Dec 27, 2012 at 23:20
  • If the OP were using Python 3, then the print statement would be a SyntaxError. Commented Dec 27, 2012 at 23:24

4 Answers 4

10

Use the int() function to convert to an integer. This will raise a ValueError when it cannot do the conversion:

try:
    price = int(price)
except ValueError as e:
    print 'invalid entry:', e
Sign up to request clarification or add additional context in comments.

Comments

4

You probably want something like this, which will catch both whether the price is a whole number and whether it is between 0 and 100, and break the loop if these conditions are fulfilled.

while True:
    price = raw_input('Please enter your marks for Maths:')
    try:
        price = int(price)
        if price < 0 or price > 100:
            raise ValueError
        break
    except ValueError:
        print "Please enter a whole number from 0 to 100"

print "The mark entered was", price

Or since you have a manageably small number of possible values you could also do something like:

valid_marks = [str(n) for n in range(101)]
price = None
while price is None:
    price = raw_input('Please enter your marks for Maths:')
    if not price in valid_marks:
       price = None
       print "Please enter a whole number from 0 to 100"

2 Comments

can I kindly ask what this line means? valid_marks = [str(n) for n in range(101)] can this be written in updated format today?
@kiritolyn it just converts the numbers in the range to strings resulting in ['0', '1', '2', ... '100']. This is still valid and commonplace in modern python.
2

First, use raw_input instead of input.

Also, place the ttt check before your input so errors display correctly:

price = 110;
ttt = 1;
while price < 0 or price > 100:
    if ttt >= 2:
        print 'This is an invalid entry, Please enter a number between 0 and 100';
    price = raw_input('Please enter your marks for Maths:');
    ttt = ttt +1;

Comments

0

I would say that the simplest thing would be to instead of doing

price=int(price)

do

price=int(110)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.