为什么Python不能识别列表元素等于输入字符串?
这有点尴尬,但我是新手,还在使用Python3.2。
以下是问题代码:
选择 | 换行 | 行号
- #a list of names
- Names = ["", "", "", "", ""]
- #initialised thus ...
- Names[1] = "Fred"
- Names[2] = "Jack"
- Names[3] = "Peter"
- Names[4] = "Kate"
- Max = 4
- Current = 1
- Found = False
- #get the name of a player from user
- TestName = input("Who are you looking for?")
- while (Found == False) and (Current <= Max):
- #next two lines put in in attempt to debug
- print(Names[Current])
- print(TestName)
- print(Names[Current] == TestName)
- if Names[Current] == TestName:
- Found = True
- else:
- Current += 1
- if Found == True:
- print("Yes, they are on my list")
- else:
- print("No, they are not there")
- #stop the wretched console disappearing
- Wait = input("Press any key")
问题是Found永远不会设置为True。例如,如果我输入"jack",则在第二次循环中,它为名称[Current]打印"Jack",为TestName打印"Jack",但条件
如果名称[当前]==测试名称
保持为假,程序继续循环。
我是要上厕所了,还是系统出了问题,还是...?其他测试显示将从以下代码片段返回True:
选择 | 换行 | 行号
- str1 = "Fred"
- str2 = "Fred"
- print(str1 == str2)
嗯,当然!那么字符串比较没有问题吗?
你能帮忙吗?