简体   繁体   中英

If/elif/else statements

My program/project is about asking the user if hes tall enough to ride a roller coaster and providing statements for a "yes" or "no" answer. My problem is if the user enters something other than yes or no I want it to say, "Enter yes or no please." My code works until I try and insert that statement. How do I insert that statement without having errors.

rc1 = input('Are you tall enough to ride this roller coaster? ')
if rc1 == 'no':
    print('Please exit the ride!')
elif rc1 == 'yes':
    rc2 = int(input('How tall are you? '))
if rc2 <= 119:
    print('You are not tall enough for this ride!')
else:
    print('Enter and enjoy the ride!')

Just a couple quick things. Just like above, a while loop is a good way to go to keep prompting a user until you get input in your desired format and your rc2 variable is defined within the if statement and not available for the comparison so it needs to be defined earlier so it is available to your second conditional. Something like this:

rc1 = input('Are you tall enough to ride this roller coaster? ')
rc2 = 0
while str.lower(rc1) not in ('yes', 'no'):
    print('Please answer yes or no')
    rc1 = input('Are you tall enough to ride this roller coaster? ')
if rc1 == 'no':
    print('Please exit the ride!')
elif rc1 == 'yes':
    rc2 = int(input('How tall are you? '))
if rc2 <= 119:
    print('You are not tall enough for this ride!')
else:
    print('Enter and enjoy the ride!')

I have added a while loop and would look something like following:

answer = None
rc2 = None
while answer not in ("yes", "no"):
    answer = input('Are you tall enough to ride this roller coaster? ')
    if answer == "yes":
        rc2 = int(input('How tall are you? '))
    elif answer == "no":
        print('Please exit the ride!')
    else:
        print("Please enter yes or no.")
    
if rc2 <= 119:
    print('You are not tall enough for this ride!')
else:
    print('Enter and enjoy the ride!')

This is my solution:

rc1 = input('Are you tall enough to ride this roller coaster? ')
rc2 = None
if rc1 == 'no':
    print('Please exit the ride!')
elif rc1 == 'yes':
    rc2 = int(input('How tall are you? '))
if rc2 != None:
    if rc2 <= 119:
        print('You are not tall enough for this ride!')
    else:
        print('Enter and enjoy the ride!')

OR

rc1 = input('Are you tall enough to ride this roller coaster? ')
if rc1 == 'no':
    print('Please exit the ride!')
elif rc1 == 'yes':
    rc2 = int(input('How tall are you? '))
    if rc2 <= 119:
        print('You are not tall enough for this ride!')
    else:
        print('Enter and enjoy the ride!')

You have to initialize the rc2 variable at the beginning. The reason you got the error was because the program was checking if rc2 was less than or equal to 119 when it didn't even know what the variable was. rc2 only exists when rc1 equals yes. For it to be used afterwards, rc2 must exist, regardless of condition.

I also got a quick cleaner solution working on it more than i should have lol. Thank you for the help

rc1 = input('Are you tall enough to ride this roller coaster? ')
while rc1 not in ('yes', 'no'):
    print('Please enter yes or no.' )
    rc1 = input('Are you tall enough to ride this roller coaster? ')
if rc1 == 'no':
    print('Please exit the ride!')
elif rc1 == 'yes':
    rc2 = int(input('How tall are you? '))
    if rc2 <= 119:
        print('You are not tall enough for this ride!')
    else:
        print('Enter and enjoy the ride!')

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