Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python If/elif/else语句_Python - Fatal编程技术网

Python If/elif/else语句

Python If/elif/else语句,python,Python,我的程序/项目是询问用户的身高是否足以坐过山车,并提供“是”或“否”的回答。我的问题是,如果用户输入的不是yes或no,我希望它说“请输入yes或no”。我的代码在我尝试插入该语句之前一直有效。如何插入该语句而不出现错误 rc1 = input('Are you tall enough to ride this roller coaster? ') if rc1 == 'no': print('Please exit the ride!') elif rc1 == 'yes':

我的程序/项目是询问用户的身高是否足以坐过山车,并提供“是”或“否”的回答。我的问题是,如果用户输入的不是yes或no,我希望它说“请输入yes或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!')
rc1=input('你够高可以坐这个过山车吗?')
如果rc1==“否”:
打印('请退出骑乘设施!')
elif rc1==“是”:
rc2=int(输入(“你有多高?”)

如果rc2我添加了一个while循环,看起来如下所示:

answer=None
rc2=无
当回答不是“是”、“否”时:
回答=输入('你够高可以坐过山车吗?')
如果回答=“是”:
rc2=int(输入(“你有多高?”)
elif答案==“否”:
打印('请退出骑乘设施!')
其他:
打印(“请输入是或否”)
如果rc2这是我的解决方案:

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!')
rc1=input('你够高可以坐这个过山车吗?')
rc2=无
如果rc1==“否”:
打印('请退出骑乘设施!')
elif rc1==“是”:
rc2=int(输入(“你有多高?”)
如果rc2!=无:

如果rc2只是一些简单的事情。正如上面所述,while循环是一种很好的方法,可以一直提示用户,直到您获得所需格式的输入,并且您的rc2变量在if语句中定义,并且不可用于比较,因此需要提前定义它,以便第二个条件变量可以使用它。大概是这样的:

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!')
rc1=input('你够高可以坐这个过山车吗?')
rc2=0
而str.lower(rc1)不在('yes','no'):
打印('请回答是或否')
rc1=输入('您的身高是否足以乘坐过山车?')
如果rc1==“否”:
打印('请退出骑乘设施!')
elif rc1==“是”:
rc2=int(输入(“你有多高?”)

如果rc2我也得到了一个快速清洁的解决方案,这比我应该得到的要多。谢谢你的帮助

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!')
rc1=input('你够高可以坐这个过山车吗?')
当rc1不在(‘是’、‘否’)时:
打印('请输入是或否')
rc1=输入('您的身高是否足以乘坐过山车?')
如果rc1==“否”:
打印('请退出骑乘设施!')
elif rc1==“是”:
rc2=int(输入(“你有多高?”)

如果rc2,您可能希望缩进第二个
if
块。在python2中,您可能不需要缩进。在Python3中,您将得到一个
类型错误
。否的答案是请退出骑乘,但代码在退出骑乘后中断。@Godfirst Yes,这是因为它尝试使用
感谢您的回答,但您错过了关于插入“输入是或否”的原始问题如果用户不回答“是”或“否”,则原始代码已起作用。此代码起作用,谢谢。一个简单的问题,str.lower做什么?好问题!str.lower将字符串(本例中的用户输入)转换为所有小写,以便与给定的值进行比较。这样,如果用户输入Yes,则在将其与Yes或no值进行比较之前,会自动使用所有小写字母。如果用户将“是”或“否”作为输入并被告知无效,则用户可能会感到困惑。
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!')