我在Repl.it上运行这个程序用于分叉目的 - 很奇怪,它似乎不起作用,因为Repl.it自动运行然后停止程序。
found = False
i = 0
capital = ["London","New Dehli","Jerusalem","Paris","Washington DC","Riyadh","Kaula Lumpur"]
user_search = ("Which capital do you wish to search for \n - ")
while found == False and i < len(capital):
if user_search == capital[i]:
print ("Capital found")
found = True
i= i+1
答案 0 :(得分:3)
你没有得到输入,所以改变它:
user_search = input("Which capital do you wish to search for \n - ")
通过适当的缩进,间距和正确的拼写,它应该是:
found = False
i = 0
capital = ["London", "New Delhi", "Jerusalem", "Paris", "Washington DC", "Riyadh", "Kuala Lumpur"]
user_search = input("Which capital do you wish to search for \n - ")
while found == False and i < len(capital):
if user_search == capital[i]:
print("Capital found")
found = True
i = i + 1
如果你没有while
循环,就像其他人说的那样,你可以使用if statement
和成员资格运算符in
。
capital = ["London", "New Delhi", "Jerusalem", "Paris", "Washington DC", "Riyadh", "Kuala Lumpur"]
user_search = input("Which capital do you wish to search for \n - ")
if user_search in capital:
print("Capital found")
答案 1 :(得分:1)
如果你想要它输出你需要输入的任何东西,我建议你不要做while循环,你可以在列表中查找一个字符串,如下所示:
capital = ["London","New Dehli","Jerusalem","Paris","Washington DC","Riyadh","Kaula Lumpur"]
user_search = input("Which capital do you wish to search for \n - ")
if user_search in capital:
print ("found it")