简体   繁体   中英

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.

I want to give a output of one of 4 possible lists, from user input, they can ask about each one, and select it. They can cancel if they want, and the function will loop to the beginning.

It should return a output of a list and a numeric, but it doesn't seem to output anything but #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. 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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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