gpt4 book ai didi

python - 仅当一个或多个项目时,如何使 for 循环工作!= 'yes'

转载 作者:太空宇宙 更新时间:2023-11-04 01:51:49 25 4
gpt4 key购买 nike

我正在完成一项 HW 任务,我在其中创建了一个带有入门问题的假俱乐部。如果任何问题的回答为“否”,则不允许此人加入。

我试过回到关于列表和循环的 og 类(class),但我找不到我想在那里做的事情。

到目前为止,这是我的代码。

# Purpose: Create a fake club that has certain requirements, ask user to
# fill out the application, and print out their answers + results.

def main():
display = input('Hi! This is your application to The Aqua Project...')
display2 = input('Read the following questions and just type y or n')

# Weird list format...
user = [input('Are you at least 18 yrs old? '),
input('Can you work with other people? '),
input('Do you like animals? '),
input('Are you okay with getting dirty sometimes? ')]



# Here's the problem, I want to print 'sorry you cant join' once...

for i in range(4):
if user[i] != 'y':
print('Sorry, but you can\'t join our club')
justToShowInCMD = input('')
i += 1
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('')

main()

当您为某些问题打上“n”时,它表示您可以加入,但对于其他问题则表示您不能加入。我不知道为什么有时它说你可以,而当你在面试中拒绝时,它不应该。

最佳答案

通常的方法是使用一个 for 循环和一个 break 和一个 else 子句:

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

关于python - 仅当一个或多个项目时,如何使 for 循环工作!= 'yes',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com