基于用户输入的基本Python函数选择系统



这是我一直在做的一个基本项目,一个基于文本的冒险,(我知道是原创的(,但我想添加一个库存选择系统。

我想给出 4 个可能列表之一的输出,从用户输入中,他们可以询问每个列表并选择它。他们可以根据需要取消,函数将循环到开头。

它应该返回列表和数字的输出,但它似乎只输出 #1 之外的任何内容。谁能看出出了什么问题?

另外,在任何人说之前,我知道它的垃圾代码,但这是我的第一个项目,任何简化和压缩它的建议将不胜感激!

inv1 = ["Sword", "Flask of Water", "Pebble", "Sheild"]
inv2 = ["Bow", "Quivver of Arrows", "Gold Necklace"]
inv3 = ["Dagger", "Bottle of Poison", "Throwing Knives"]
inv4 = ["Spellbook: Cast Fireball", "Spellbook: Heal", "Spellbook: Control Skelleton"]
emptyinvt = []
z = 0
def invt(a):
if a == "1":
print (inv1)
ans1 = input("Do you want to take loadout 1? ")
if ans1 == "yes":
return 1
elif ans1 == "no":
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
else:
print ("Not a option!")
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
elif a == "2":
print (inv2)
ans2 = input("Do you want to take loadout 2? ")
if ans2 == "yes":
return 2
elif ans2 == "no":
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
else:
print ("Not a option!")
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
elif a == "3":
print (inv3)
ans3 = input("Do you want to take loadout 3? ")
if ans3 == "yes":
return 3
elif ans3 == "no":
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
else:
print ("Not a option!")
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
elif a == "4":
print (inv4)
ans4 = input("Do you want to take loadout 4? ")
if ans4 == "yes":
return 4
elif ans4 == "no":
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
else:
print ("Not a option!")
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
else:
print ("Not a option!")
invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
int_invent = invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))
print (int_invent)
print ("player INVT " + str(int_invent))
if int_invent == 1:
plrinvt = list(set(inv1 + emptyinvt))
elif int_invent == 2:
plrinvt = list(set(inv2 + emptyinvt))
elif int_invent == 3:
plrinvt = list(set(inv3 + emptyinvt))
elif int_invent == 4:
plrinvt = list(set(inv4 + emptyinvt))
print ("Player Inventory %d has been selected" % int_invent)
print ("It contains: " + str(plrinvt))

如果第一个输入有效,您的代码工作正常。但是,递归调用失败,因为返回值为 None。当用户在输入后说"否"并且给出无效值时,就会发生递归调用。您以递归方式调用该函数,但如果选择到主线程,它不会返回所选值。一种解决方案是在主线程上使用条件循环来确保选择正确的序列。你可以按如下方式循环你的函数:

inv1 = ["Sword", "Flask of Water", "Pebble", "Sheild"]
inv2 = ["Bow", "Quivver of Arrows", "Gold Necklace"]
inv3 = ["Dagger", "Bottle of Poison", "Throwing Knives"]
inv4 = ["Spellbook: Cast Fireball", "Spellbook: Heal", "Spellbook: Control Skelleton"]
emptyinvt = []
z = 0
def invt(a):
if a == "1":
print (inv1)
ans1 = input("Do you want to take loadout 1? ")
if ans1 == "yes":
return 1
elif ans1 == "no":
return None
else:
print ("Not a option!")
return None
elif a == "2":
print (inv2)
ans2 = input("Do you want to take loadout 2? ")
if ans2 == "yes":
return 2
elif ans2 == "no":
return None
else:
print ("Not a option!")
return None
elif a == "3":
print (inv3)
ans3 = input("Do you want to take loadout 3? ")
if ans3 == "yes":
return 3
elif ans3 == "no":
return None
else:
print ("Not a option!")
return None
elif a == "4":
print (inv4)
ans4 = input("Do you want to take loadout 4? ")
if ans4 == "yes":
return 4
elif ans4 == "no":
return None #we return None so that the while loop works
else:
print ("Not a option!")
return None
else:
print ("Not a option!")
return None
int_invent = None
while(int_invent is None): #If there was a problem in invt it just relaunches it with the same query
int_invent = invt(input("Your king offers you three bundles of tools for your journey, which do you take?"))

print ("player INVT " + str(int_invent))
if int_invent == 1:
plrinvt = list(set(inv1 + emptyinvt))
elif int_invent == 2:
plrinvt = list(set(inv2 + emptyinvt))
elif int_invent == 3:
plrinvt = list(set(inv3 + emptyinvt))
elif int_invent == 4:
plrinvt = list(set(inv4 + emptyinvt))
print(int_invent)
print ("Player Inventory %d has been selected" % int_invent)
print ("It contains: " + str(plrinvt))

我这里有一个建议,我已经尝试过你的游戏。您应该使其更加用户友好。例如

'Your king offers you three bundles of tools for your journey, which do you take?'

,您应该在末尾添加可能的答案。这会让玩起来更容易一些。例:

'Your king offers you three bundles of tools for your journey, which do you take? (1,2,3)'. 

我试图帮助你,但我不明白你的问题和你想做什么。请详细说明。

最新更新