Python 如果只有一个或多个项目,如何使for循环工作!=';是';

Python 如果只有一个或多个项目,如何使for循环工作!=';是';,python,python-3.x,Python,Python 3.x,我正在做一个HW任务,在那里我创建了一个带有入会问题的假俱乐部。如果其中任何一个问题的答案是“否”,则该人不允许加入 我已经试着回到og关于列表和循环的课程,但是我找不到我在那里尝试做什么 这是到目前为止我的代码 #目的:创建一个有一定要求的假俱乐部,请用户 #填写申请表,打印出他们的答案和结果。 def main(): display=input('您好!这是您对Aqua项目的应用程序…') display2=输入('阅读以下问题并键入y或n') #奇怪的列表格式。。。 用户=[输入('你至少

我正在做一个HW任务,在那里我创建了一个带有入会问题的假俱乐部。如果其中任何一个问题的答案是“否”,则该人不允许加入

我已经试着回到og关于列表和循环的课程,但是我找不到我在那里尝试做什么

这是到目前为止我的代码

#目的:创建一个有一定要求的假俱乐部,请用户
#填写申请表,打印出他们的答案和结果。
def main():
display=input('您好!这是您对Aqua项目的应用程序…')
display2=输入('阅读以下问题并键入y或n')
#奇怪的列表格式。。。
用户=[输入('你至少18岁吗?'),
输入(“你能和其他人一起工作吗?”),
输入('你喜欢动物吗?'),
输入('有时你能忍受脏衣服吗?')]
#问题是,我想打印一次“对不起,你不能加入”。。。
对于范围(4)中的i:
如果用户[i]!='y':
打印('对不起,您不能加入我们的俱乐部')
justToShowInCMD=输入(“”)
i+=1
其他:
打印(“”)
打印('祝贺您,您已满足我们的所有要求!')
打印('我们将很快发送电子邮件,讨论我们的团队何时开始')
打印('将会面以帮助拯救一些动物!')
打印('同时,请访问我们的网站:
TheAquaProject.com')
justToShowInCMD=输入(“”)
main()
当你对一些问题打“n”时,表示你可以加入,但对其他问题,则表示你不能加入。我不知道为什么有时候它会说你可以,当你在面试中说“不”时,它不应该这样做。

如果一个“不”表示拒绝,你可以在打印拒绝信息后添加
break
退出循环。就像:

范围(4)内的i的
:
如果用户[i]!='y':
打印('对不起,您不能加入我们的俱乐部')
justToShowInCMD=输入(“”)

#i+=1#有几种方法可以做到这一点:

  • 使用一个标志变量,只在末尾输出。(如果第一个响应是
    ,则效率稍低)
  • 当用户响应
    no
    时,使用标志变量和while循环退出。(可能会有点混淆)
  • 使用内置的
    any
    方法。(可能会混淆,不推荐)
  • flag=True
    对于范围(4)中的i:
    如果用户[i]!='y':
    flag=False#用户对某些内容的回答为“否”,请将该标志设置为False
    if标志:#用户已对所有问题回答“是”
    # 
    else:#用户对某些内容的回答为“否”
    # 
    
    您的代码中有一些小点需要更改:

    # 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? ')]
        # you define a variable inside function main
        # and assign a list to it
        # this variable will not be visible outside
        # you should just return it
        return user
    
    
    # Here's the problem, I want to print 'sorry you cant join' once...
    # call your function before you test the answer
    # and assign the the result of the function to a
    # variable you can use in your check
    user= main()
    
    # define a flag variabe to
    # see if the answers are ok according your check
    ok= True
    for i in range(4):
        if user[i].lower()[:1] != 'y':
            # you could also use your original code
            # in the check. The code above is an example
            # how you could make sure the user can enter
            # upper/lower case letters and also "yes" 
            # and "y" [:1] cuts off 1 character
            # if the string is non-empty, otherwise it
            # returns an empty string
            print('Sorry, but you can\'t join our club')
            justToShowInCMD = input('')
            i += 1
            # memorize that some question wasn't ok
            ok= False
            # I guess here you might want to exit the loop?
            # so use a break, otherwise the other answers
            # would be checked as well and the message
            # output several times per user in some cases
            break        
    if ok:
        # this code here doesn't belong in the loop body 
        # I guess. It should be executed after all questions
        # have been checked positive (which is only known
        # after the loop has been executed)
        # So here we are sure the answers were yes, because
        # otherwise we would have set ok to False
        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 you call your function here, you can't check
    # the result of the input() calls because
    # by the time you check it it has not been entered
    

    通常的方法是使用
    break
    else
    子句执行
    for
    循环:

    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')
    
    关于OP
    main()
    的注释:
    • 编程的一个关键点是代码使用效率。
      • 不必要时不要重复调用函数(例如
        输入
        打印
    • 有几种方法可以解决您的问题。
      • 其他答案集中在原始的
        用户
        列表上,并重复调用
        输入
      • 一旦执行了
        user
        ,它实际上就变成了
        y
        和/或
        n
        值的列表,然后用循环将其解包以检查值
      • user
        list方法的另一个问题是,它要求在取消资格之前回答所有问题。如果有40个问题呢?我会生气的
      • 顺便说一句,列表可以按如下方式解压缩:
        for value in user:
        。不需要使用python按索引处理列表

    更新了
    main()
    实现:
    def打印列表(值:列表):
    “”“打印列表的值”“”
    对于值中的值:
    打印(值)
    def main():
    “”“遍历调查问卷”“”
    #顶部的变量
    简介=['嗨!这是你对Aqua项目的申请…',
    '阅读以下问题并键入y或n']
    问题=[“你至少18岁吗?”,
    “你能和其他人一起工作吗?”,
    “你喜欢动物吗?”,
    “有时候你能忍受脏衣服吗?”]
    final=['\n总体而言,您已满足我们的所有要求!',
    “我们将很快发送一封电子邮件,讨论我们的团队,
    “会聚在一起帮助拯救一些动物!”,
    “同时,请访问我们的网站www.TheAquaProject.com”]
    打印列表(简介)
    对于i,枚举中的问题(问题,开始=1):
    回答=输入(问题)
    如果响应='n':#可以替换为!='y'
    打印(“对不起,你不能加入俱乐部!”)
    打破
    如果i==len(问题):
    打印列表(最终版)
    
    更新的
    main()
    上的注释:
  • 不要多次调用
    print
    ,而是将文本存储在列表中,然后调用
    print\u list
    函数进行打印。
    • 将自定义函数分开
    • 自定义函数应执行一个函数
    • 值:列表
      这是一个
      类型
      提示,它告诉
      数据类型
      打印列表
  • 参数应该是什么
  • “”
    :使用docstring
  • 函数之间的双空格:
  • main()
  • 问题
    显而易见,它只是来自
    用户的问题
    ,作为
    列表
    ,而不调用
    输入
  • 使用
    for循环
    解包
    问题
    ,然后使用。
    • 有很多
    • i
      enumerate
      一起计数
    • questions = ["Are you at least 18 yrs old?", "Can you work with other people?", "Do you like animals?", "Are you okay with getting dirty sometimes?"] answers = list() for question in questions: user_answer = input(f"[y/n] {question}: ").lower() answers.append(user_answer) if "n" in answers: print("Sorry, you can't join our club.") else: print("Congrats! You are in!!") # you can print your desired messages as well.
                 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('')