简体   繁体   中英

How do i print an output if the value entered is not an integer

I am trying to do two things here.

I want to print "value entered is not a valid age" if the input is not a number in this code:

age = float(input (' enter your age:  '))
if 0 <age < 16:
    print "too early for you to drive"
if  120 >= age >= 95:
    print 'Sorry; you cant risk driving'
if age <= 0:
    print 'Sorry,' ,age, 'is not a valid age'
if age > 120: 
    print 'There is no way you are this old. If so, what is your secret?'
if 95 > age >= 16:
    print 'You are good to drive!'

Also, how can I repeat this program once it is done?

You could check whether input is a valid digit with isdigit method of str . For multiple times you could wrap your code to a function and call it as much as you want:

def func():
    age = input (' enter your age:  ')

    if age.isdigit():
        age = float(age)

        if 0 <age < 16:
            print "too early for you to drive"
        if  120 >= age >= 95:
            print 'Sorry; you cant risk driving'
        if age <= 0:
            print 'Sorry,' ,age, 'is not a valid age'
        if age > 120: 
            print 'There is no way you are this old. If so, what is your secret?'
        if 95 > age >= 16:
            print 'You are good to drive!'
    else:
        print "value entered is not a valid age"

func()

For make this code runnig every time you should add a loop , like while loop , and add a break whenever you want end this loop or add a condition for example run 10 times,and if you want check the input is float , you can add a try section:

counter = 0
while counter < 10:
    age = raw_input (' enter your age:  ')
    try :
        age = float(age)
        if 0 <age < 16:
            print "too early for you to drive"
        if  120 >= age >= 95:
            print 'Sorry; you cant risk driving'
        if age <= 0:
            print 'Sorry,' ,age, 'is not a valid age'
        if age > 120: 
            print 'There is no way you are this old. If so, what is your secret?'
        if 95 > age >= 16:
            print 'You are good to drive!'
        counter -= 1
    except ValueError:
         print "value entered is not a valid age"

It is also a good idea to arrange your cases in order, to make it easier to see what is happening (and to make sure you didn't leave an unhandled gap):

while True:
    age = input("Enter your age (or just hit Enter to quit): ").strip()

    if not age:
        print("Goodbye!")
        break       # exit the while loop

    try:
        # convert to int
        age = int(age)
    except ValueError:
        # not an int!
        print("Value entered is not a valid age")
        continue    # start the while loop again

    if age < 0:
        print("Sorry, {} is not a valid age.".format(age))
    elif age < 16:
        print("You are too young to drive!")
    elif age < 95:
        print("You are good to drive!")
    elif age <= 120:
        print("Sorry, you can't risk driving!")
    else:
        print("What are you, a tree?")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM