-4

Whenever an output should result in a change of shield, the program just says 'You have 5 shields' followed by 'You Win'. I think I might have made a mistake when passing over the variables from one function to the next. Thank you in advance for the help!

def main():
    while TakeTurn(word,shield) == False:
        if shield == 1:
            word1 = "shield"
        else:
            word1 = "shields"
        print ("You have", shield, word1,)
    if shield < 1:
        print ("Sorry! You ran out of shields! You lose!")
    else:
        print ("You win")
#This function is the actual 'game' and will deterine what happens to the character    
def TakeTurn(word1,shield1):
    time.sleep(1.5)
    #This means that when the user reaches 0 shields, they lose.
    if shield1 < 1:
        return True
    #Whatever the user inputs will not actually affect the outcome
    print ("You have reached", word1 ,"junction.\nDo you want to turn left (L), turn right (R) or go straight ahead(S)?")
    turning = input()
    #This is a simple instruction that means that the first time you come to a junction, it will say 'a junction' but the second time it will say 'another junction'
    word1 = "another"
    #This 'if' statement means that the program will only continue if the user has inputed the letters 'L', 'R' or 'S'
    elif turning not in ["L","R","S","l","r","s"] :
        time.sleep (0.7)
        print ("Sorry, I didn't understand that")
        TakeTurn()
    else:
        choice = randint (1,10)
    #This is just going to display a random message which will affect the outcome
    time.sleep (1)
    if choice == 1:
        print ("You have found the exit!")
        return True
    elif choice == 2:
        print ("You have found a shield!")
        time.sleep(1)
        shield1 = shield1 +1
        return False
    elif choice == 3:
        print ("You have found two shields!")
        time.sleep(1)
        shield1 = shield1 +2
        return False
    elif choice == 4:
        print ("You have found three shields!")
        time.sleep(1)
        shield1 = shield1 +3
        return False
    elif choice == 5:
        print ("A fairy has jumped into your pants!")
        time.sleep(2)
        print ("You lose two shields")
        time.sleep(1)
        shield1 = shield1 -2
        return False
    elif choice == 6:
        treasurechest(shield1)
        return False
    elif choice == 7:
        print ("You have tripped over a log!")
        time.sleep(2)
        print ("You lose a shield")
        time.sleep(1)
        shield1 = shield1 -1
        return False
    elif choice == 8:
        print ("An angry teenager is staring at you in the eye.")
        time.sleep(2.5)
        print ("He uses laziness...")
        time.sleep(1.5)
        print ("It's super effective!")
        time.sleep(1)
        print ("You lose three shields")
        time.sleep(1)
        shield1 = shield1 -3
        return False
    elif choice == 9:
        print ("You have encountered an ogre...")
        time.sleep(1.5)
        print ("He bashes you over the head with a steel bar")
        time.sleep(2)
        print ("You lose two shields")
        time.sleep(1)
        shield1 = shield1 -2
        return False
    else:
        print ("A goblin aproaches and says the following:")
        time.sleep(2)
        goblin(shield1)
        return False
Joseph
  • 1
  • 5

1 Answers1

2

You should refactor main and TakeTurn to make shield and word explicit arguments and return values. This prevents relying on scope for access to variables, without having to use global (which is usually a bad sign):

def main(shield, word):
    while True:
        shield, word, finished = TakeTurn(shield, word)
        if finished:
            break
        word1 = "shield" if shield == 1 else "shields"
        print ("You have {0} {1}".format(shield, word1))
    if shield < 1:
        print ("Sorry! You ran out of shields! You lose!")
    else:
        print ("You win")

And have TakeTurn return multiple values accordingly, e.g.:

elif choice == 3:
    print ("You have found two shields!")
    time.sleep(1)
    shield1 = shield1 + 2
    return shield1, word1, False

You could make things neater by making choices a list of dictionaries and picking randomly from it:

choices = [{"texts": [("You have found two shields!", 1)], 
            "shield change": 2, "return": False}, 
           {"texts": [("You have encountered an ogre...", 1.5), 
                      ("He bashes you over the head with a steel bar", 2),
                      ("You lose two shields", 1)],
            "shield change": -2, "return": False},
           ...]

Then your main section, instead of all the elifs, becomes simply:

choice = random.choice(choices)
for text, sleep_time in choice['texts']:
    print(text)
    time.sleep(sleep_time)
shield1 += choice['shield change']
return shield1, word1, choice['return']
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437