3 回答
TA贡献1780条经验 获得超4个赞
执行此操作的常用方法是带有 a和子句的for循环:breakelse
for answer in user:
if answer != 'y':
print('Sorry')
break
else:
print('Congratulations')
或any()功能:
if any(answer != 'y' for answer in user):
print('Sorry')
else:
print('Congratulations')
TA贡献1851条经验 获得超5个赞
假设您range(4)基于 的长度迭代 4 次(使用 )user,您可以简单地执行以下操作:
if 'n' or 'no' in user:
print('Sorry, but you can\'t join our club')
justToShowInCMD = input('')
else:
print('')
print('Congratulations, you have met all of our requirements!')
print('We will send an email soon to discuss when our team')
print('will meet up to help save some animals!')
print('In the meantime, visit our website at
TheAquaProject.com')
justToShowInCMD = input('')
您可以修改if条件以适应其他形式的否定答案,例如'N' or 'No'. 您不需要遍历user.
TA贡献1852条经验 获得超7个赞
如果一个“否”表示拒绝,您可以break在打印拒绝信息后添加退出循环。就像:
for i in range(4):
if user[i] != 'y':
print('Sorry, but you can\'t join our club')
justToShowInCMD = input('')
# i += 1 # <<<<<<<<<<<<<<<<<< this code may be not needed here
break # <<<<<<<<<<<<<<<<<< where to add break
else:
print('')
print('Congratulations, you have met all of our requirements!')
print('We will send an email soon to discuss when our team')
print('will meet up to help save some animals!')
print('In the meantime, visit our website at
TheAquaProject.com')
justToShowInCMD = input('')
或者你可以使用一个变量来表示是否拒绝,就像:
toDecline = False
for i in range(4):
if user[i] != 'y':
toDecline = True
if toDecline:
print('Sorry, but you can\'t join our club')
justToShowInCMD = input('')
else:
print('')
print('Congratulations, you have met all of our requirements!')
print('We will send an email soon to discuss when our team')
print('will meet up to help save some animals!')
print('In the meantime, visit our website at
TheAquaProject.com')
justToShowInCMD = input('')
添加回答
举报