简体   繁体   English

if/elif/else 语句

[英]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.就像上面一样,while 循环是 go 不断提示用户的好方法,直到您以所需格式输入并且您的 rc2 变量在 if 语句中定义并且不可用于比较,因此需要提前定义它适用于您的第二个条件。 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:我添加了一个 while 循环,看起来如下所示:

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.您必须在开始时初始化rc2变量。 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是否小于或等于 119,而它甚至不知道变量是什么。 rc2 only exists when rc1 equals yes. rc2仅在rc1等于 yes 时存在。 For it to be used afterwards, rc2 must exist, regardless of condition.为了以后使用它, rc2必须存在,无论条件如何。

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!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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