所以我不确定我在这里做错了什么,但是如果他们输入了' bloof'如果出现拼写错误,它会说"找不到项目"但即使你输入" blood"它仍然会调用"项目未找到"
如果你拿出elif声明,输入" blood"调用该项目,但使用elif语句,它总是说"项目未找到"
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: ")
shopchoice = shopchoice.title()
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
elif shopchoice not in text2:
print("Item not found.")
shopchoice = input("Please pick another item? ")
答案 0 :(得分:3)
您的方法和解决方案可以解决您的方法中的错误。
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但有成千上万的记录,它会很慢。
答案 1 :(得分:2)
因为您只是在输入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? ")
就个人而言,我会这样写:
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.")
每次找不到某个项目时它会循环播放并重新提示用户。
答案 2 :(得分:1)
您没有使用列表中的所有可用项目检查您的输入,只是第一个。您需要循环遍历所有这些内容,以确保您的输入不在其中任何一个中。此外,如果输入无效,您需要递归回输入检查。类似的东西:
# 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
答案 3 :(得分:0)
问题是你的for循环,你循环遍历整个列表并对每个列表应用检查。由于数组中有两个项目而您只测试一个项目,因此每次都会得到两个结果。
要解决此问题,只需在循环中添加break
即可。看起来像这样:
for text2 in shopitemsF:
if shopchoice in text2:
print(text2)
break
因为你不想要"找不到项目"对于不匹配的数组中的每个项目,我建议改为创建一个变量来存储是否找到该项目,并结合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? ")