无法突破while循环,即搜索列出的值

时间:2015-05-05 13:19:15

标签: python while-loop

所以我很确定我完全错了,但我不知道怎么做。我打算使用数组,但不知道如何。我对Python不那么蓬松的一面很陌生(在学校学习它) 因此,如果我能够掌握这一点,我将不胜感激。

所以我想尝试一些练习设计和编程,我想用这样的图表进行神奇宝贝类型比较:

http://i.kinja-img.com/gawker-media/image/upload/s--6gT1hiPW--/fxovveduxtomv4srnqk1.png

所以,我的想法是输入类型,然后输出它的优点和缺点。但对于我的生活,我无法超越选择类型的初始阶段。

这是我的第一行:

Elements = "Normal","Fire","Water","Grass","Electric","Bug","Flying","Ground","Rock","Posion","Dragon","Dark","Fairy","Psychic","Steel","Fighting","Ice"
type1 = input("Please input a type")
while type1 != Elements:
    type1 = input("Please input a real type")

print("Good Job, this part works!") # But it doesn't get to this point...

我很抱歉这么糟糕,但每个人都天真开始吧? 提前感谢您提供给我的任何帮助!

3 个答案:

答案 0 :(得分:2)

首先,您需要一个列表来存储所有类型,然后重复询问用户输入,然后将输入与预定义的元素列表进行匹配,如果找到匹配则中断while循环否则你只是继续,这是在这种情况下要遵循的简单算法。

elements = ["Normal","Fire","Water","Grass","Electric","Bug","Flying","Ground","Rock","Posion","Dragon","Dark","Fairy","Psychic","Steel","Fighting","Ice"]
#Initialized the various types in a list.
while True:    #Infinite loop
    type1 = input("Please input a real type")   #Taking input from the user
    if type1 in elements:    #Checking if the input is already present in the given list of elements.
        print("Good Job, this part works!")
        break

答案 1 :(得分:1)

我认为您要检查type1是否在Elements中,而不是它是否等于它。 Elements是一个字符串元组,type1只是一个字符串。这两件事永远不会平等。

您可以测试是否使用in关键字作为:

while( type1 not in Elements ):
    type1 = raw_input( "Enter a valid type" )

答案 2 :(得分:1)

你试图看一个单词是否等于一个列表,这永远不会是真的,你想看看单词是否在列表中

elements = "Normal","Fire","Water","Grass","Electric","Bug","Flying","Ground","Rock","Posion","Dragon","Dark","Fairy","Psychic","Steel","Fighting","Ice"
type1 = input("Please input a type")
while type1 not in elements:
    type1 = input("Please input a real type")

print("Good Job, this part works!") # But it doesn't get to this point...