gpt4 book ai didi

python - 为什么我的代码找不到数组中存在的某个字符串?

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:44 25 4
gpt4 key购买 nike

我正在编写一个类似于迷你文本冒险游戏的程序,但是我的代码似乎遇到了一些问题。我对整个编程还比较陌生,所以如果你能深入解释你的答案,那将会有很大的帮助。

代码如下:

#text_adventure_game.py
#python_ver == 3.5

verbs = ["get",
"put",
"drop",
"use",
"enter",
"leave",
"walk",
"search",
"attack",
"defend"]

nouns = ["journal",
"magnifier",
"glasses",
"knife",
"kite",
"table",
"chair",
"key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
counter = 0
for token in user_input.split():
print(token)
print(counter)
if(counter==0):
if(token not in verbs):
print("I did not understand the verb entered.")
#removing square brackets and single quote marks from the array
print("Please use:", ", ".join(verbs))
counter = counter + 1
elif(token in verbs):
print("Recognized verb entered.")
counter = counter + 1
if(counter==1):
if(token not in nouns):
print("I did not understand the noun entered.")
print("Please use:", ", ".join(nouns))
counter = counter + 1
elif(token in nouns):
print("Recognized verb entered.")
counter = counter + 1

我的问题是它无法识别我在“名词”数组中输入的名词。

这是它的编译方式:

>>> 
What would you like to do?
get journal
get
0
Recognized verb entered.
I did not understand the noun entered.
Please use: journal, magnifier, glasses, knife, kite, table, chair, key
journal
2

如果有更有效的方法来做这样的事情,那也会有所帮助。

谢谢。

最佳答案

不需要 for 循环和计数器。您可以更简单地执行以下操作:

verbs = ["get",
"put",
"drop",
"use",
"enter",
"leave",
"walk",
"search",
"attack",
"defend"]

nouns = ["journal",
"magnifier",
"glasses",
"knife",
"kite",
"table",
"chair",
"key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
action, obj = user_input.split()

if(action not in verbs):
print("I did not understand the verb entered.")
print("Please use:", ", ".join(verbs))
else:
print("Recognized verb entered.")

if(obj not in nouns):
print("I did not understand the noun entered.")
print("Please use:", ", ".join(nouns))
else:
print("Recognized noun entered.")

如果想让失败后重复执行,可以将代码放在 while block 下。

无论如何,原始代码中的问题是,如果第一个动词正确,则必须插入继续:

verbs = ["get",
"put",
"drop",
"use",
"enter",
"leave",
"walk",
"search",
"attack",
"defend"]

nouns = ["journal",
"magnifier",
"glasses",
"knife",
"kite",
"table",
"chair",
"key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
counter = 0
for token in user_input.split():
print("1", token)
print(counter)
if(counter==0):
if(token not in verbs):
print("I did not understand the verb entered.")
#removing square brackets and single quote marks from the array
print("Please use:", ", ".join(verbs))
counter = counter + 1
elif(token in verbs):
print("Recognized verb entered.")
counter = counter + 1
continue
if(counter==1):
if(token not in nouns):
print("I did not understand the noun entered.")
print("Please use:", ", ".join(nouns))
counter = counter + 1
elif(token in nouns):
print("Recognized verb entered.")
counter = counter + 1

关于python - 为什么我的代码找不到数组中存在的某个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35866848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com