0

I want to make a game that includes the component of 'lives'. I would like to start the game with 3 lives. Each time the user dies, the lives remaining decreases by 1 and the game restarts with the new number of lives remaining.

PROBLEM: If I play the game and lose a life, it always says "2 lives remaining" even if I only have 1 or 9 lives. Why doesn't the number of lives remaining never go below 2?

This is portion of my code:

import random

def main():

    livesRemaining = 3

    print "Welcome to Escape From The Shackles! "
    print "3 transparent vials with liquid lie in front of you on a table. One of them is green, another is yellow, yet another is red."
    colors = ["green", "red", "yellow"]
    random.shuffle(colors)

    color = raw_input("Which one of the three colors do you choose to drink?")
    color = color.lower()

    if color == (colors[0]):
        print "The liquid seeps into your system and poisons you. You die!"
        livesRemaining = livesRemaining - 1
        print "You have " + str(livesRemaining) + " lives remaining."
        main()


    elif color == (colors[1]):
        print "Your head begins to spin and you become unconscious! Next thing you know, you're in a raft in a river. No soul is present nearby..."
        print "After paddling for 15 minutes in the river, you encounter a crossroads! "
        right_or_left = raw_input("Do you choose to paddle right or left?")
        right_or_left = yellow_right_or_left.lower()

        if yellow_right_or_left == "right":
            print "You take a right turn and continue to paddle. A mighty waterfall confronts you."
            print "You die!"
            livesRemaining = livesRemaining - 1
            print "You have " + str(livesRemaining) + " lives remaining."
            main()
Alfe
  • 56,346
  • 20
  • 107
  • 159
Agastya Rao
  • 13
  • 1
  • 2
  • 1
    After dying you always call `main()` again and in `main()`'s first statement, `livesRemaining` is reset to 3. – Alfe Jul 03 '18 at 10:06
  • Oh, okay. I understand the problem. Do you know how I can fix it? I've been trying ways to do it for so long, but I haven't got there yet. I'd really appreciate your help. Thank you! – Agastya Rao Jul 03 '18 at 10:11
  • This question deals with global vs. local variables in python pretty well: https://stackoverflow.com/q/423379/406423 – MadMike Jul 03 '18 at 11:47

2 Answers2

0

You want to wrap it into a while loop instead of calling itself\

import random

livesRemaining = 3

def main():
    global livesRemaining

    print "Welcome to Escape From The Shackles! "
    while True:
        if livesRemaining == 0:
            break

        print "3 transparent vials with liquid lie in front of you on a table. One of them is green, another is yellow, yet another is red."
        colors = ["green", "red", "yellow"]
        random.shuffle(colors)

        color = raw_input("Which one of the three colors do you choose to drink?")
        color = color.lower()

        if color == (colors[0]):
            print "The liquid seeps into your system and poisons you. You die!"
            livesRemaining -= 1
            print "You have " + str(livesRemaining) + " lives remaining."
            main()


        elif color == (colors[1]):
            print "Your head begins to spin and you become unconscious! Next thing you know, you're in a raft in a river. No soul is present nearby..."
            print "After paddling for 15 minutes in the river, you encounter a crossroads! "
            right_or_left = raw_input("Do you choose to paddle right or left?")
            right_or_left = yellow_right_or_left.lower()

            if yellow_right_or_left == "right":
                print "You take a right turn and continue to paddle. A mighty waterfall confronts you."
                print "You die!"
                livesRemaining -= 1
                print "You have " + str(livesRemaining) + " lives remaining."
        print "Game Over"
Reroute
  • 265
  • 1
  • 9
  • I couldn't thank you enough! It now works! I had been trying for so long... – Agastya Rao Jul 03 '18 at 10:36
  • How can I add a 'play again' feature. I tried, but whenever I typed 'y', it kept prompting me if I would like to play again. This is what I tried: if livesRemaining == 0: print "GAME OVER!" again = raw_input("Would you like to play again? If so, type 'y'.") if again == "y": continue else: break – Agastya Rao Jul 04 '18 at 09:17
  • if livesRemaining == 0: print "GAME OVER!" again = raw_input("Would you like to play again? If so, type 'y'.") if again == "y": livesRemaining = 3 else: break – Reroute Jul 04 '18 at 09:46
  • Fantastic! Thanks! – Agastya Rao Jul 04 '18 at 16:52
0

I would re-organize the code something like this:

def welcome_message(livesRemaining):
    print "Welcome to Escape From The Shackles!"
    print "You have", livesRemaining, "lives remaining."

def vial_scene():
    print "3 transparent vials with liquid lie in front of you on a table." \
          " One of them is green, another is yellow, yet another is red."
    colors = ["green", "red", "yellow"]
    random.shuffle(colors)
    color = raw_input("Which one of the three colors do you choose to drink?")
    color = color.lower()
    if color == colors[0]:
        print "The liquid seeps into your system and poisons you. You die!"
        return 'dies'
    elif color == colors[1]:
        print "Your head begins to spin and you become unconscious!"
        return 'head_spin'

def river_scene():
    print "Next thing you know, you're in a raft in a river." \
          " No soul is present nearby ..."
    print "After paddling for 15 minutes in the river, you encounter" \
          " a crossroads!"
    right_or_left = raw_input("Do you choose to paddle right or left?")
    right_or_left = right_or_left.lower()
    if right_or_left == "right":
        print "You take a right turn and continue to paddle." \
              " A mighty waterfall confronts you."
        print "You die!"
        return 'dies'

def main():
    livesRemaining = 3
    while livesRemaining > 0:
        welcome_message(livesRemaining)
        vial_result = vial_scene()
        if vial_result == 'dies':
            livesRemaining -= 1
            continue  # jump to beginning of while loop
        elif vial_result == 'head_spin':
            river_result = river_scene()
            if river_result == 'dies':
                livesRemaining -= 1
                continue  # jump to beginning of while loop
            else:
                print "The river doesn't kill you."
        else:
            print "The vial doesn't kill you and gives you no head spin."
        print "You survived the two problems (vial and river)."
        break  # to leave the while loop
Alfe
  • 56,346
  • 20
  • 107
  • 159