简体   繁体   English

在 python if 语句中使用列表

[英]using list in python if statements

I am trying to code a function that gives the user results based on the input.我正在尝试编写一个 function 代码,根据输入为用户提供结果。 So basically what my function is supposed to do is to make the user input the values for day and based on that proceed towards asking the user their name.所以基本上我的 function 应该做的是让用户输入一天的值,并在此基础上继续询问用户他们的名字。 If even one of the answers doesn't match the required value, I want my code to be display invalid entry.My code is:如果其中一个答案与所需值不匹配,我希望我的代码显示无效条目。我的代码是:

days=[monday, tuesday, wednesday, thursday, friday, saturday, sunday]
def my_function():
    for items in days:
        day=input('what is the day')
        if input==items:
            print('okay')
            for stuff in names:
            name= input("what is your name?")
            if input==stuff:
                print('great')
            else:
                print('invalid entry')  
        else:
            print('invalid entry')

please be gentle as i am new to python.请温柔,因为我是 python 的新手。 thanks in advance提前致谢

if input==items:

This should be:这应该是:

if day==items:

Comparing input , which is a function , to a string makes very little sense.input (即function )与字符串进行比较没有多大意义。

Smae problem with your next if .你的下一个if有问题。

You should probably refactor things so you have a reusable function to have the user choose from a list.您可能应该重构一些东西,以便您拥有一个可重用的 function 让用户从列表中进行选择。


def input_choice(prompt, choices):
    while True:
        response = input(prompt)
        if response in choices:
            return response  # bail out from the infinite loop
        print("invalid entry, try one of %s" % choices)


days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]

day = input_choice("what is the day?", days)
name = input("what is your name?")
print("hi %s, you chose the day %s" % (name, day))

replace this替换这个

if input==items:

with this:有了这个:

if day == items:

and replace this:并替换它:

if input==stuff:

with this:有了这个:

if name == staff:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM