1

(Python 2.7.10) So i am a beginner in python, just started learning about a week ago. I need some help with writing the code commented on lines 3 and 5.If the user enters a word instead of a numerical value then I need the program to tell them error and to restart. I commented the program to make it easier to understand. The program works just fine otherwise. Thank you.

## Ask user for age
age = input("Please enter your age.(numerical value)")
## If input is not a numerical value then tell the user "Error. Enter a numerical value"

## Restart program to let the user try again.

## If age is less than 18 then tell them they are too young
if age < 18:
    print (" Access denied. Sorry, you are not old enough.")
## If the user is 18 then grant them access 
elif age == 18:
    print ("Acess granted. You are just old enough to use this program!")
## If user is any age above 18 then grant them access
else:
    print ("Access granted.")

2 Answers 2

1

his is a way to make sure you get something that can be interpreted as integer from the user:

while True:
    try:
        # in python 3:
        # age = int(input('Please enter your age.(numerical value)'))
        # in python 2.7
        age = int(raw_input('Please enter your age.(numerical value)'))
        break
    except ValueError:
        print('that was not an integer; try again...')

the idea is to try to cast the string entered by the user to an integer and ask again as long as that fails. if it checks out, break from the (infinite) loop.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you. What is the point of using int(raw_input) instead of just using input in the first place? Doesn't input only accept integers anyways?
input() always accept integer and raw_input() will always accept as string. int(raw_input()) will try to convert the input back to integer. But if the input is not a valid integer, it will fail and raise exception. You can do it either way.
@krisk the point is: if you define a variable before the input statement; say a=17 and the user input is the string a then no NameError is raised (as a can be interpreted as a valid expression) and age is assigned to 17. which may not be what you want.
@hiro oops! Didn't take that in account. Will be fine while running in script mode, I hope. :)
0

Change your age input part to this:

 #keep asking for age until you get a numerical value
 while True: 
     try:
         age=int(raw_input("Please enter your age.(numerical value)"))
         break
     except ValueError:
         print "Error. Enter a numerical value"

4 Comments

I tried replacing it and "break" on line 5 is not correctly indented. I tried to indent it in different spots and I can not get it to work. There's an error in your program: unexpected unindent
did you copy and pasted? Try writing on your own with proper indentation. It will be fine.
I have indented it properly now.
Seems to work perfectly now. Thank you for all the help

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.