猿问

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

我正在做一个硬件任务,在那里我创建一个带有入门问题的假俱乐部。如果任何问题的回答为“否”,则不允许此人加入。


我试过回到关于列表和循环的 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”时,它表示您可以加入,但对于其他问题,它表示您不能加入。我不知道为什么有时当你在面试中拒绝时它说你可以,但不应该。


烙印99
浏览 99回答 3
3回答

Helenr

执行此操作的常用方法是带有 a和子句的for循环:breakelsefor answer in user:    if answer != 'y':        print('Sorry')        breakelse:    print('Congratulations')或any()功能:if any(answer != 'y' for answer in user):    print('Sorry')else:    print('Congratulations')

江户川乱折腾

假设您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.

慕姐4208626

如果一个“否”表示拒绝,您可以break在打印拒绝信息后添加退出循环。就像:for i in range(4):&nbsp; &nbsp; &nbsp; &nbsp; if user[i] != 'y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Sorry, but you can\'t join our club')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; justToShowInCMD = input('')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # i += 1&nbsp; &nbsp;# <<<<<<<<<<<<<<<<<< this code may be not needed here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; # <<<<<<<<<<<<<<<<<< where to add break&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Congratulations, you have met all of our requirements!')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('We will send an email soon to discuss when our team')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('will meet up to help save some animals!')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('In the meantime, visit our website at&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TheAquaProject.com')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; justToShowInCMD = input('')或者你可以使用一个变量来表示是否拒绝,就像:toDecline = Falsefor i in range(4):&nbsp; &nbsp; &nbsp; &nbsp; if user[i] != 'y':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toDecline = Trueif toDecline:&nbsp; &nbsp; print('Sorry, but you can\'t join our club')&nbsp; &nbsp; justToShowInCMD = input('')else:&nbsp; &nbsp; print('')&nbsp; &nbsp; print('Congratulations, you have met all of our requirements!')&nbsp; &nbsp; print('We will send an email soon to discuss when our team')&nbsp; &nbsp; print('will meet up to help save some animals!')&nbsp; &nbsp; print('In the meantime, visit our website at&nbsp;&nbsp; &nbsp; TheAquaProject.com')&nbsp; &nbsp; justToShowInCMD = input('')
随时随地看视频慕课网APP

相关分类

Python
我要回答