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('')
添加回答
舉報