为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

烙印99 2022-05-24 12:52:14
我正在做一个硬件任务,在那里我创建一个带有入门问题的假俱乐部。如果任何问题的回答为“否”,则不允许此人加入。我试过回到关于列表和循环的 og 课程,但我找不到我想要在那里做的事情。到目前为止,这是我的代码。# 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”时,它表示您可以加入,但对于其他问题,它表示您不能加入。我不知道为什么有时当你在面试中拒绝时它说你可以,但不应该。
查看完整描述

3 回答

?
Helenr

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


查看完整回答
反对 回复 2022-05-24
?
江户川乱折腾

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.


查看完整回答
反对 回复 2022-05-24
?
慕姐4208626

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


查看完整回答
反对 回复 2022-05-24
  • 3 回答
  • 0 关注
  • 98 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信