-1

Im trying to produce a very simple adventure text game and i tried to produce a minimal reproducible example in this case(my code may appear to be slightly long but i cant delete anymore or else it becomes difficult to understand).

The problem with my codes right now is that it wont proceed to go to the forest when the user is prompt to choose a choice = input("Go to the shop, go to the tavern, go to the forest: ").

And the other problem is that the user must enter tavern 2 times in order to proceed and after that it will show the error "dexterity not defined" at the line if dexterity >= 5: even though i have earlier listed it as part of the dictionary list...

gold = int(100)
crossbow = int(50)
spell = int(35)
potion = int(35)
inventory = ["sword", "armor", "potion"]

print("Welcome hero")
name = input("What is your name: ")
print("Hello", name,)
# role playing program
#
# spend 30 points on strenght, health, wisdom, dexterity 
# player can spend and take points from any attribute

# library contains attribute and points
attributes = {"strength": int("0"),
             "health": "0",
             "wisdom": "0",
             "dexterity": "0"}

pool = int(30)
choice = None
print("The Making of a Hero !!!")
print(attributes)
print("\nYou have", pool, "points to spend.")

while choice != "0":
    # list of choices
    print(
    """
    Options: 

    0 - End
    1 - Add points to an attribute
    2 - remove points from an attribute
    3 - Show attributes
    """
    )
    choice = input("Choose option: ")
    if choice == "0":
        print("\nYour hero stats are:")
        print(attributes)
    elif choice == "1":
        print("\nADD POINTS TO AN ATTRIBUTE")
        print("You have", pool, "points to spend.")



    choice = input("Go to the shop, go to the tavern, go to the forest: ")
    while choice != "shop" or "tavern" or "forest":
        print("Not accepted")
        print("What do you wish to do?")
        print("please input shop, tavern, forest.")
        choice = input("Go to the shop, go to the tavern, go to the forest: ")

        if choice == "tavern":
            print("You enter the tavern and see a couple of drunken warriors singing, a landlord behind the bar and a dodgy figure sitting at the back of the tavern.")
            tavernChoice = input("Would you like to talk to the 'drunken warriors', to the 'inn keeper', approach the 'dodgy figure' or 'exit'")

            if tavernChoice == "drunken warriors":
                print("You approach the warriors to greet them.")
                print("They notice you as you get close and become weary of your presence.")
                print("As you arrive at their table one of the warriors throughs a mug of ale at you.")
            if dexterity >= 5:
                print("You quickly dodge the mug and leave the warriors alone")
            else:
                print("You are caught off guard and take the mug to the face compleatly soaking you.")
                print("The dodgy figure leaves the tavern")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
cena
  • 410
  • 1
  • 4
  • 12
  • 2
    You should have while condition somting like this. **while choice not in("shop", "tavern","forest"):** – krishna Mar 01 '20 at 11:43
  • @krishna ah i didn't know in python is possible to put 3 conditions at once in while condition.. – cena Mar 01 '20 at 11:46
  • So if you put like this then your problem is solved? – krishna Mar 01 '20 at 11:47
  • 1
    Your condition right now is interpreted as `while (choice != "shop") or ("tavern") or ("forest")` This means the condition will **always be true** because a non-empty string is truthy – Tomerikoo Mar 01 '20 at 11:48

2 Answers2

1

You have definied "dexterity" as part of "attributes" so you need to refer to is as attributes['dexterity'].

daghemo
  • 156
  • 6
  • so which means to say if dexterity >= 5: should be attributes['dexterity']>-5? – cena Mar 01 '20 at 11:42
  • 1
    yes. you should write dexterity as attributes['dexterity']. that is because you have declared dexterity as an element of attributes. – daghemo Mar 01 '20 at 11:46
  • after i put your suggestion, i got the error "'>=' not supported between instances of 'str' and 'int'" at if attributes['dexterity'] >= 5: – cena Mar 01 '20 at 11:49
  • Because you saved the dict as `"dexterity": "0"` instead of `"dexterity": 0` @cena – Tomerikoo Mar 01 '20 at 11:50
1

Your first problem is the input validation line:

while choice != "shop" or "tavern" or "forest":

The expression is evaluated as:

while (choice != "shop") or ("tavern") or ("forest"):

So the condition will always be true because a non-empty string is truthy. You could write something like:

while choice not in ("shop", "tavern", "forest"):

Your second problem is that you keep all your code inside the input validation. You have a while loop to keep asking the user for input until a valid response is entered, which is good. But you need to remember that once a valid input was entered, the loop will terminate (because now choice is actually in ("shop", "tavern", "forest")). So your code should be something like:

    choice = input("Go to the shop, go to the tavern, go to the forest: ")
    while choice not in("shop", "tavern","forest"):
        print("Not accepted")
        print("What do you wish to do?")
        print("please input shop, tavern, forest.")
        choice = input("Go to the shop, go to the tavern, go to the forest: ")

    if choice == "tavern":
        ....

Regarding the 'dexterity' error. It is not a variable, but a key in your dict so change to:

if attributes['dexterity'] >= 5:
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61