简体   繁体   English

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

[英]Basic Python Function Selection System Based On User Input

this is a basic project I've been working on, a text based adventure, (original, I know) but I wanted to add a inventory select system to it.这是我一直在做的一个基本项目,一个基于文本的冒险,(我知道是原创的)但我想向它添加一个库存 select 系统。

I want to give a output of one of 4 possible lists, from user input, they can ask about each one, and select it.我想给出 4 个可能列表之一的 output,从用户输入中,他们可以询问每个列表和 select 它。 They can cancel if they want, and the function will loop to the beginning.他们可以根据需要取消,function 将循环到开头。

It should return a output of a list and a numeric, but it doesn't seem to output anything but #1.它应该返回一个列表和一个数字的 output,但它似乎没有 output 除了#1。 Can anyone see what is wrong?任何人都可以看到有什么问题吗?

Also, before anyone says, I know its shite code, but this is my first project, any advice to simplify and condense it would be appreciated!另外,在任何人说之前,我知道它的垃圾代码,但这是我的第一个项目,任何简化和压缩它的建议将不胜感激!

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))

Your code is working fine if the first input is valid.如果第一个输入有效,您的代码工作正常。 Your recursive call however fails because the return value is None.但是,您的递归调用失败,因为返回值为 None。 Recursive calls occur when the user says no after an input and when an invalid value is given.当用户在输入后说不并且给出无效值时,会发生递归调用。 You recursively call the function but it does not return the value selected if selected to the main thread.您递归调用 function 但如果选择主线程,它不会返回选择的值。 One solution is using a conditional loop on the main thread to ensure a proper sequence is selected.一种解决方案是在主线程上使用条件循环来确保选择正确的序列。 you could just loop your function as follows:你可以循环你的 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))

I have a suggestion here, I have tried your game.我有一个建议,我已经尝试过你的游戏。 You should make it more user friendly.你应该让它对用户更友好。 For example例如

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

, you should add possible answers to the end. ,您应该在最后添加可能的答案。 It would make it a bit easier to play.这样玩起来会容易一些。 Example:例子:

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

I was trying to help you out but I don't understand your question and what you want to make.我试图帮助你,但我不明白你的问题和你想要做什么。 Please elaborate.请详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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