Python 检查列表中用户原始输入的元素数

Python 检查列表中用户原始输入的元素数,python,list,elements,raw-input,Python,List,Elements,Raw Input,我是python新手,在尝试一些想法时,我遇到了以下问题: 我希望用户为一个蛋糕输入5种配料,并将它们存储在一个列表中,然后将列表返回给用户。 我告诉用户告诉我5种成分。 现在我想让python检查用户是否真的给了我5种成分,或者给了他们一条错误消息。 这就是我到目前为止得到的 def recipe(): #create a list to store the recipe ingredients. print "Enter 5 ingredients that could po

我是python新手,在尝试一些想法时,我遇到了以下问题: 我希望用户为一个蛋糕输入5种配料,并将它们存储在一个列表中,然后将列表返回给用户。 我告诉用户告诉我5种成分。 现在我想让python检查用户是否真的给了我5种成分,或者给了他们一条错误消息。 这就是我到目前为止得到的

def recipe():
    #create a list to store the recipe ingredients.
    print "Enter 5 ingredients that could possibly go into a cake: "
    recipeList = []
    ingredients = raw_input("> ")
    recipeList = recipeList.append(ingredients)

    recipeList = [ingredients]

    print recipeList

    if len(ingredients) = 5:
        print "Thanks"
    elif len(ingredients) > 5:
    print "That's too much"
    elif len(ingredients) < 5:
    print "That's too little"
    else:
        print "There's something wrong!"

recipe()
def配方():
#创建一个列表来存储配方成分。
打印“输入可能放入蛋糕中的5种成分:”
recipeList=[]
成分=原始输入(“>”)
recipeList=recipeList.append(配料)
recipeList=[成分]
打印回执列表
如果len(成分)=5:
打印“谢谢”
elif len(成分)>5:
打印“太多了”
elif len(成分)<5:
打印“太少了”
其他:
打印“有问题!”
配方()

如果所有成分都是一个单词,那么它们将在逻辑上由一个空格分隔,因此您只需在每个空格上拆分输入字符串。或者,您可以指定每个成分都需要用逗号或某种分隔符分隔,并且
split(“,”)

相等运算符为
==

if len(recipeList ) == 5:

您需要使用
split
函数,因为原始输入不会用空格或任何其他分隔符分隔输入

recipeList = ingredients.split()
另外,这个比较应该是双等号,否则它就是赋值。此外,它应该比较列表的长度,而不是用户输入

if len(recipeList) == 5:
下面是一行:

def recipe():
    #create a list to store the recipe ingredients.
    print "Enter 5 ingredients that could possibly go into a cake: "
    recipeList = []
    line = raw_input("> ")         # changed line

    ingredients = line.split()     # changed line, converts the input in an array
                                   # (input separated by spaces)

    recipeList = recipeList.append(ingredients)

    recipeList = [ingredients]

    print recipeList

    if len(ingredients) == 5:      # added the double equal
       print "Thanks"
    elif len(ingredients) > 5:
       print "That's too much"
    elif len(ingredients) < 5:
       print "That's too little"
    else:
       print "There's something wrong!"

recipe()
def配方():
#创建一个列表来存储配方成分。
打印“输入可能放入蛋糕中的5种成分:”
recipeList=[]
行=原始输入(“>”)#更改行
Components=line.split()#已更改的行,转换数组中的输入
#(输入以空格分隔)
recipeList=recipeList.append(配料)
recipeList=[成分]
打印回执列表
如果len(配料)=5:#加上两个相等的
打印“谢谢”
elif len(成分)>5:
打印“太多了”
elif len(成分)<5:
打印“太少了”
其他:
打印“有问题!”
配方()

这些行中有很多是多余的。你所需要的就是这样的东西*:

def recipe():
    """Create a list to store the recipe ingredients."""

    print "Enter 5 ingredients that could possibly go into a cake: "
    ingredients = raw_input("> ").split()
    print ingredients

    if len(ingredients) == 5:
        print "Thanks"
    elif len(ingredients) > 5:
        print "That's too much"
    elif len(ingredients) < 5:
        print "That's too little"
    else:
        print "There's something wrong!"

recipe()
它基本上做两件事:

  • 使用
    原始输入获取输入

  • 使用拆分空格上的输入。结果将是子字符串(成分)列表

  • 另外,如果你想知道的话,我把你函数顶部的注释变成了一个恰当的注释


    *注:我假设成分之间用空格隔开。但是,如果您希望使用不同的分隔符(如逗号)将其分隔,则可以为
    str.split
    指定一个特定的分隔符:

    ingredients = raw_input("> ").split(",")
    

    如果len(配料)==5#缺少doble等式,为什么要执行
    recipeList.append(配料)
    ,然后执行
    recipeList=[配料]
    ?首先将其添加到列表中,然后用一个元素的新列表替换该列表。你不可能真的希望这两件事都发生。你想用空格分隔5种成分吗?还是逗号?或者你想一次问他们1个(在单独的行上,有单独的提示),直到你得到5个,或者直到他们在没有输入任何内容的情况下点击“回车”键?或者…?例如,发酵粉是蛋糕中非常常见的成分。如果你在空格上拆分,你会把它算作两个成分。这仍然不能解决问题,因为
    len(components)
    将是
    components
    中的字符数,而不是
    recipeList
    中的字数。对不起,我在代码中添加了注释,但每个人都这么说。代码可以修改很多,只需要修改一下最小值。它对recipeList做了很多奇怪的事情,我不知道为什么。
    ingredients = raw_input("> ").split()
    
    ingredients = raw_input("> ").split(",")