使用for循环

时间:2017-06-23 16:05:47

标签: python list python-3.x loops for-loop

我有以下代码:

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for i in list:
        if list.index(eat)==eat:        
            break
        else:
            break

    print("We have that in tray:", list.index(eat))
main()

在VB.Net中,这在For循环中很容易工作,这将是惯用的方法。为什么不在这里?此外,这不是stackoverflow上的另一个问题的副本,其中用户提供了做类似事情的替代方法/ pythonic建议。

出于教学目的,我需要使用for循环,并更正上面给出的结构。

如果用户输入的内容不是列表中的内容,我如何添加将打印“该项不在列表中”的条件逻辑。我正在寻找原始代码的最简单修复。

我确实尝试了以下方法,但产生了逻辑错误。有了解决方案。

尝试#1:

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for i in list:
        if list.index(eat)==eat:        
            break
        else:
            print("that is not in the list")

    print("We have that in tray:", list.index(eat))
main()

错误:

>>> 
What do you wanna eat?jelly
that is not in the list
that is not in the list
that is not in the list
We have that in tray: 1
>>> 

尝试2

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for i in list:
        if list.index(eat)==eat:        
            break
        else:
            break
    print("that is not in the list")

    print("We have that in tray:", list.index(eat))
main()

错误:

>>> 
What do you wanna eat?jelly
that is not in the list
We have that in tray: 1
>>> 

尝试#3:

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for i in list:
        if list.index(eat)==eat:        
            break
        elif list.index(eat)!=eat:
            print("that is not in the list")

    print("We have that in tray:", list.index(eat))
main()

错误:

>>> 
What do you wanna eat?jelly
that is not in the list
that is not in the list
that is not in the list
We have that in tray: 1
>>> 

2 个答案:

答案 0 :(得分:1)

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
if eat in list:
    print("We have that in tray:", list.index(eat))
else:
    print("That is not in the list")

main()

答案 1 :(得分:1)

这是:

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for element in list:
        index = list.index(element)
        if element == eat:
          print("We have that in tray:", index)
          break
          # If the element is found, then print the message and stop the loop

        elif index == len(list) - 1:
          # Index starts form 0, then if you have a list with n elements
          # and you reach the element with index n-1, you have reached
          # the last element.
          print("that is not in the list")
          # if the element is the last in the list, and it's not
          # equal to the user input, then the user input is not in the
          # list. Print the error message.

main()

使用enumerate

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for index,element in enumerate(list):
        if element == eat:
          print("We have that in tray:", index)
          break
        elif index == len(list) - 1:
          print("that is not in the list")

main()