Python Elif语句在不应出现时出现';t在列表中查找项目时
因此,我不确定我在这里做错了什么,但我希望这样,如果他们键入“bloof”时出现拼写错误,它会说“未找到项”,但在这里使用elif语句,即使您键入“blood”,它仍然称为“未找到项” 如果您取出elif语句,键入“blood”将调用该项,但使用elif语句时,它总是显示“未找到项”Python Elif语句在不应出现时出现';t在列表中查找项目时,python,python-3.x,Python,Python 3.x,因此,我不确定我在这里做错了什么,但我希望这样,如果他们键入“bloof”时出现拼写错误,它会说“未找到项”,但在这里使用elif语句,即使您键入“blood”,它仍然称为“未找到项” 如果您取出elif语句,键入“blood”将调用该项,但使用elif语句时,它总是显示“未找到项” 因为在输入if语句之前只检查第一项 你应该这样做: res = "" for text2 in shopitemsF: if shopchoice in text2: res = text2
因为在输入if语句之前只检查第一项 你应该这样做:
res = ""
for text2 in shopitemsF:
if shopchoice in text2:
res = text2
if res != "":
print(res)
else:
print("Item not found.")
shopchoice = input("Please pick another item? ")
# start by initializing the input:
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
while True: # main decision loop, repeat until we have a valid input
shopchoice = shopchoice.title() # capitalize the first letter
found = None # holds our found element, if any
for text2 in shopitemsF: # loop through all of the elements in the list
if shopchoice in text2: # check if the input is in the current element
found = text2 # it is, store it in found for use bellow
break # item found, no need to search further, comment if you want the last item
if found: # we've found an element in the previous loop
print(found) # print it...
break # exit the main decision loop
else: # item was not found
print("Item not found.")
shopchoice = input("Please pick another item? ") # request for another input, repeat
就我个人而言,我会这样写:
shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300",
"Bloodcursed Sword: 200 Damage, Cost: 950"]
item_not_found = True
while item_not_found:
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
shopchoice = shopchoice.title()
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
item_not_found = False
break
if item_not_found:
print("Item not found.")
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
break
因此,每次找不到某个项时,它都会循环并重新提示用户。因为在输入if语句之前,您只检查第一个项 你应该这样做:
res = ""
for text2 in shopitemsF:
if shopchoice in text2:
res = text2
if res != "":
print(res)
else:
print("Item not found.")
shopchoice = input("Please pick another item? ")
# start by initializing the input:
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
while True: # main decision loop, repeat until we have a valid input
shopchoice = shopchoice.title() # capitalize the first letter
found = None # holds our found element, if any
for text2 in shopitemsF: # loop through all of the elements in the list
if shopchoice in text2: # check if the input is in the current element
found = text2 # it is, store it in found for use bellow
break # item found, no need to search further, comment if you want the last item
if found: # we've found an element in the previous loop
print(found) # print it...
break # exit the main decision loop
else: # item was not found
print("Item not found.")
shopchoice = input("Please pick another item? ") # request for another input, repeat
就我个人而言,我会这样写:
shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300",
"Bloodcursed Sword: 200 Damage, Cost: 950"]
item_not_found = True
while item_not_found:
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
shopchoice = shopchoice.title()
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
item_not_found = False
break
if item_not_found:
print("Item not found.")
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
break
因此,每次找不到某个项目时,它都会循环并重新向用户提示。您的方法和下面的解决方案修复了您方法中的错误,但不会扩展
found = False
for text2 in shopitemsF:
if shopchoice in text2:
found = True
break
if not found:
print("Item not found.")
shopchoice = input("Please pick another item? ")
else:
print("Item found")
我说的不是scalabe,因为如果您在
shopitemsF
中有N个项目,并且每个项目的平均长度为M
,则此搜索将是O(NM)-可管理的小N和小M,但有数千条记录,它将非常缓慢。您的方法和下面的解决方案,它修复了您方法中的错误,但不会扩展
found = False
for text2 in shopitemsF:
if shopchoice in text2:
found = True
break
if not found:
print("Item not found.")
shopchoice = input("Please pick another item? ")
else:
print("Item found")
我说的不是scalabe,因为如果您在
shopitemsF
中有N个项目,并且每个项目的平均长度为M
,则此搜索将是O(NM)-可管理的小N和小M,但有数千条记录,速度将非常慢。您没有检查列表中所有可用项目的输入,就第一个。您需要遍历所有这些文件,以确保您的输入不在其中任何一个文件中。此外,如果输入无效,则需要返回到输入检查。比如:
res = ""
for text2 in shopitemsF:
if shopchoice in text2:
res = text2
if res != "":
print(res)
else:
print("Item not found.")
shopchoice = input("Please pick another item? ")
# start by initializing the input:
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
while True: # main decision loop, repeat until we have a valid input
shopchoice = shopchoice.title() # capitalize the first letter
found = None # holds our found element, if any
for text2 in shopitemsF: # loop through all of the elements in the list
if shopchoice in text2: # check if the input is in the current element
found = text2 # it is, store it in found for use bellow
break # item found, no need to search further, comment if you want the last item
if found: # we've found an element in the previous loop
print(found) # print it...
break # exit the main decision loop
else: # item was not found
print("Item not found.")
shopchoice = input("Please pick another item? ") # request for another input, repeat
您没有检查列表中所有可用项的输入,只检查第一项。您需要循环遍历所有这些参数,以确保输入不在其中任何一个。此外,如果输入无效,则需要返回到输入检查。比如:
res = ""
for text2 in shopitemsF:
if shopchoice in text2:
res = text2
if res != "":
print(res)
else:
print("Item not found.")
shopchoice = input("Please pick another item? ")
# start by initializing the input:
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
while True: # main decision loop, repeat until we have a valid input
shopchoice = shopchoice.title() # capitalize the first letter
found = None # holds our found element, if any
for text2 in shopitemsF: # loop through all of the elements in the list
if shopchoice in text2: # check if the input is in the current element
found = text2 # it is, store it in found for use bellow
break # item found, no need to search further, comment if you want the last item
if found: # we've found an element in the previous loop
print(found) # print it...
break # exit the main decision loop
else: # item was not found
print("Item not found.")
shopchoice = input("Please pick another item? ") # request for another input, repeat
问题是您的for循环,您正在遍历整个列表并对每个列表应用检查。由于数组中有两个项目,并且您只测试一个项目,因此每次都会返回两个结果。 要解决这个问题,只需在循环中添加一个
break
。看起来像这样:
shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300",
"Bloodcursed Sword: 200 Damage, Cost: 950"]
item_not_found = True
while item_not_found:
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
shopchoice = shopchoice.title()
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
item_not_found = False
break
if item_not_found:
print("Item not found.")
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
break
由于您不希望数组中不匹配的每个项目都出现“item not found”(项目未找到),因此我建议您创建一个变量来存储是否找到该项目,并结合while循环,在未找到该项目时再次触发商店,如果玩家不想购买,则创建一个结束循环的条目:
shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300",
"Bloodcursed Sword: 200 Damage, Cost: 950"]
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
while True:
shopchoice = shopchoice.title()
if shopchoice == "End":
break
found = False
for text2 in shopitemsF:
if shopchoice in text2:
found = text2
break
if found != False:
print(found)
break
print("Item not found.")
shopchoice = input("Please pick another item? ")
问题是您的for循环,您正在遍历整个列表并对每个列表应用检查。由于数组中有两个项目,并且您只测试一个项目,因此每次都会返回两个结果。 要解决这个问题,只需在循环中添加一个
break
。看起来像这样:
shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300",
"Bloodcursed Sword: 200 Damage, Cost: 950"]
item_not_found = True
while item_not_found:
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
shopchoice = shopchoice.title()
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
item_not_found = False
break
if item_not_found:
print("Item not found.")
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
break
由于您不希望数组中不匹配的每个项目都出现“item not found”(项目未找到),因此我建议您创建一个变量来存储是否找到该项目,并结合while循环,在未找到该项目时再次触发商店,如果玩家不想购买,则创建一个结束循环的条目:
shopitemsF = ["Ghostblade: 150 Damage, Cost: 700", "Thunderblade: 120 Damage, Cost: 300",
"Bloodcursed Sword: 200 Damage, Cost: 950"]
shopchoice = input("Please choose an item from the shop by typing part of its name: ")
while True:
shopchoice = shopchoice.title()
if shopchoice == "End":
break
found = False
for text2 in shopitemsF:
if shopchoice in text2:
found = text2
break
if found != False:
print(found)
break
print("Item not found.")
shopchoice = input("Please pick another item? ")
我真的不知道你的意思。因此,在你的代码中输入for循环,检查第一个项目是否包含
血液
,然后告诉用户没有找到该项目。与通知用户之前检查所有项目不同。我真的不知道你的意思。因此,在代码中输入for循环,检查第一个项目是否包含血液
,然后告诉用户未找到该项目。而不是在通知用户之前检查所有项目。您正在检查列表中的每个项目。如果第一个项目不包含血液,那么else语句将启动并要求您提供另一个输入。您要做的是在检查列表中的所有项目后检查是否找到匹配项。另外请注意,不需要elif。这只是一个else。哦,这是有道理的。我不认为它会这样做。你正在检查列表中的每一项。如果第一个项目不包含血液,那么else语句将启动并要求您提供另一个输入。您要做的是在检查列表中的所有项目后检查是否找到匹配项。另外请注意,不需要elif。这只是另一个例子。哦,这是有道理的,我不认为它是这样做的。是的,这起作用了,现在我知道为什么了,感谢@Thanassis。很高兴能提供帮助。是的,这起作用了,现在我知道为什么了,感谢@Thanassis。很高兴能提供帮助。