[英]Basic Python Function Selection System Based On User Input
這是我一直在做的一個基本項目,一個基於文本的冒險,(我知道是原創的)但我想向它添加一個庫存 select 系統。
我想給出 4 個可能列表之一的 output,從用戶輸入中,他們可以詢問每個列表和 select 它。 他們可以根據需要取消,function 將循環到開頭。
它應該返回一個列表和一個數字的 output,但它似乎沒有 output 除了#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。 當用戶在輸入后說不並且給出無效值時,會發生遞歸調用。 您遞歸調用 function 但如果選擇主線程,它不會返回選擇的值。 一種解決方案是在主線程上使用條件循環來確保選擇正確的序列。 你可以循環你的 function 如下:
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)'.
我試圖幫助你,但我不明白你的問題和你想要做什么。 請詳細說明。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.