繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM