Python Forum
Errors in simple text adventure game?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Errors in simple text adventure game?
#1
Hello,
I am doing a simple text adventure game for school and so far I have the following.
On line 61 (elif choice == 2:) which in the function prompt 3, I am getting an unexpected
unindent error. I assume it has something to do with the indentation but I can't figure it out.

Can anyone plug this into IDLE and debug it?
Please keep in mind I am new to the language and I'm completely aware that this code may be unprofessional or over complicated.

Thanks!


import time
global playerhealth
global enemyhealth
enemyhealth = 25.0
playerhealth = 50.0
bosshealth = 100.0
global guessed

def prompt2():
    print("""
          You walk into a cave.
          You are scared.
          Do you chose to continue or turn around?
          Type '1' or '2'.

          1: Turn around
          2: Continue
          """)
    choice = int(input("What will you do?: "))
    
    try:
        if choice == 1:
            youLose()
        elif choice == 2:
            print("Good choice, " + name + ", let's continue!" )
            prompt3()
        else:
            print("Not valid!")
            prompt2()
    except NameError:
        print("You need to type a single number to reflect your choice!")
        prompt2()

def prompt3():
    print("""
          You run into a skeleton ghoul.
          You also hear the mysterious ghost's laugh echo through the cave.
          What is your weapon of choice?

          1: Sword
          2: Bow
          3: Staff
          """)
    choice = int(input("How will you fight?: "))
    
    try:
        if choice == 1:
            print("""
                  Great! Will you attack fast or slow?

                  1: Fast
                  2: Slow             
                  """)
            choiceSword = int(input("Fast or slow?: "))
            try:
                if choiceSword == 1:
                    prompt4()
                if choiceSword == 2:
                    print("You attacked too slow and he stabbed you!")
                    youLose()       
        elif choice == 2:
            print("Good choice, " + name + ", let's continue!" )
            prompt4()
        elif choice == 3:
            print("""
                  You chose a staff, a versatile weapon.
                  Will you choose to attack first?
                  Or defend? Or fight by hand?

                  1: Attack
                  2: Defend
                  3: Fight By Hand
                  """)
            choiceStaff = int(input("How will you use the staff?: "))
            try:
                if choiceStaff == 1:
                    prompt4()
                elif choiceStaff == 3:
                    youLose()
                elif choiceStaff == 2:
                    while enemyhealth >= 0:        
                        print("""
                              The skeleton breaks your shield and damages you 4.6 health!
                              """)
                        playerhealth -= 4.6
                        print("You have " + str(playerhealth) + " health remaining!")
                        attackStaff = raw_input("Do you wish to attack? ('yes' or 'no'): ")
                        if attackStaff == yes:
                            enemyhealth -= 26
                            print("""
                                  You attacked the enemy with a critical hit.
                                  You killed him.
                                  """)
                            prompt4()                    
                        elif attackStaff == no:
                            print("Your being too timid!")
                            youLose()
                        else:
                            print("""
                                  You need to type either 'yes' or 'no'!
                                  Rolling back to start of phase!
                                  """)
                            prompt3()
                            
                            
                            
                        
            except NameError:
               print("You need to type a single number to reflect your choice!")
               prompt3() 
                        
            
        else:
            print("Not valid!")
            prompt2()
    except NameError:
        print("You need to type a single number to reflect your choice!")
        prompt2()
        
def prompt4():
    print("""
          The ghost can only be stopped by his defeat or a guess.
          Guess the ghost!

          1: Arnold
          2: Sam
          3: Pluto
          """)
    guess = int(input("Who is the ghost(1, 2, or 3)?: "))

    try:
        if guess == 1:
            youWin()
        if guess == 2:
            guessed = 2
            print("Wrong! Moving on!")
            prompt5()
        if guess == 3:
            guessed = 3
            print("Wrong! Moving on!")
            prompt5()
    except NameError:
        print("Type an integer to reflect your answer!")
        prompt4()

def prompt5():
    print("""
          You come to a lake. Do you jump in or turn to your left?

          1: Jump In
          2: Turn Left
          """)
    prompt5answer = int(input("Do you jump or turn?: "))

    try:
        if(prompt5answer == 1):
            print("You drowned!")
            youLose()
        elif(prompt5answer == 2):
            print("Good choice! Moving along....")
            prompt6()
    except NameError:
        print("Type an integer to reflect your answer!")
        prompt5()

def prompt6():
    print("""
          The ghost can only be stopped by his defeat or a guess.
          Guess the ghost!
          """)
    if guessed == 2:
        print("""

          1: Arnold
          2: Pluto
          """)
    if guessed == 3:
        print("""

          1: Arnold
          2: Sam
          """)
    guess2 = int(input("Guess the ghost: "))

    try:
        if guess2 == 1:
            youWin()
        elif guess2 == 2:
            print("Wrong! You notice the rocks on the walls start to rumble.")
            print("You see a white light slowly form into a figure. Could it be?")
            time.sleep(2)
            bossFight()
    except NameError:
        print("Type an integer to reflect your answer!")
        prompt6()

def bossFight():
    print("""

          The ghost is revealeed to you as Arnold!         
          """)
    print("The boss still has " + str(bosshealth) + " remaining!")
    print("You still have " + str(playerhealth) + " remaining! Good luck!")

    while playerhealth > 0 and bosshealth > 0:
        print("""

              Arnold glares at you. Do you choose to attack or defend?

              1: Attack
              2: Defend
              """)
        choiceBoss = int(input("Do you attack or defend?")
        try:
            if choiceBoss == 1:
                print("You attack the boss for 23.6 health!")
                bosshealth -= 23.6
                print("The boss has " + str(bosshealth) + " remaining!")
                print()
            elif choiceBoss == 2:
                print("The boss attacks you for 10.0 health!")
                playerhealth -= 10.0
                print("You have " + str(player) + " remaining!")
                print()

            if playerhealth < 0:
                print("You have been defeated.")
                youLose()
            if bosshealth < 0:
                youWin()
            else:
                continue
                         
        except NameError:
        print("Type an integer to reflect your answer!")
        prompt4()
    

def hitBoss():                        
    print("You damage the boss!")
    bosshealth -= 10
    if bosshealth > 0:                        
        print("The boss still has " + str(bosshealth) + " remaining!")
        
    
def youLose():
    print("Nice try, " + name + ", try again next time!")

def youWin():
    print("Congratulations!")
    print("You either killed or guessed the ghost, Arnold!")
    print("Good job, " + name + "! Come back to the castle soon!")

def start():
    print("Welcome to the Castle! The castle is haunted by a ghost!")
    time.sleep(1)
    print("For centuries, many have tried to figure out who it was.")
    time.sleep(1)
    print("No one has. Until now...")
    time.sleep(2)
    print()
    global name
    name = raw_input("Enter your name: ")  
    startPrompt()

def startPrompt():
    global prompt1
    prompt1 = raw_input("Type 'start' to start the game: ")

    if prompt1 == "start":
        print("Starting game.....")
        print()
        time.sleep(2)
        prompt2()
    else:
        print()
        print("You're dumb! Type 'start' stupid!")
        startPrompt()

start()
Reply
#2
Could you copy/paste the entire traceback?

*edit* nevermind, I found it. Here's your snippet:
Quote:
            try:
                if choiceSword == 1:
                    prompt4()
                if choiceSword == 2:
                    print("You attacked too slow and he stabbed you!")
                    youLose()       
        elif choice == 2:

A try without a matching except is meaningless, so it's assumed that your elif is part of the if choiceSword == 2: block, and, in THAT case, the elif isn't indented properly. Adding an except clause should fix the error.
Reply
#3
(Apr-06-2018, 05:52 PM)nilamo Wrote: Could you copy/paste the entire traceback?

*edit* nevermind, I found it. Here's your snippet:
Quote:
            try:
                if choiceSword == 1:
                    prompt4()
                if choiceSword == 2:
                    print("You attacked too slow and he stabbed you!")
                    youLose()       
        elif choice == 2:

A try without a matching except is meaningless, so it's assumed that your elif is part of the if choiceSword == 2: block, and, in THAT case, the elif isn't indented properly. Adding an except clause should fix the error.

So basically, if I move the except on line 108 up a bit and then fix the indentation I should be good?
Reply
#4
No, because then you'd have a different try: that didn't have an except anymore.

In order to try to avoid confusing nested pathways like this, it's generally a good idea to have only a few lines in a try block, like 2 or 3 max. Any more than that, and you're probably trying to do too much with one exception handler.
Reply
#5
(Apr-07-2018, 11:03 PM)nilamo Wrote: No, because then you'd have a different try: that didn't have an except anymore.

In order to try to avoid confusing nested pathways like this, it's generally a good idea to have only a few lines in a try block, like 2 or 3 max. Any more than that, and you're probably trying to do too much with one exception handler.

Ok, so now I'm getting another error about a variable I have?

Traceback:
Traceback (most recent call last):
File "H:\Python\MCA Game Loev Krupa.py", line 226, in <module>
start()
File "H:\Python\MCA Game Loev Krupa.py", line 194, in start
startPrompt()
File "H:\Python\MCA Game Loev Krupa.py", line 204, in startPrompt
prompt2()
File "H:\Python\MCA Game Loev Krupa.py", line 19, in prompt2
prompt3()
File "H:\Python\MCA Game Loev Krupa.py", line 37, in prompt3
choiceSword()
File "H:\Python\MCA Game Loev Krupa.py", line 217, in choiceSword
prompt4()
File "H:\Python\MCA Game Loev Krupa.py", line 60, in prompt4
youWin()
File "H:\Python\MCA Game Loev Krupa.py", line 178, in youWin
if deadBoss == False:
NameError: global name 'deadBoss' is not defined


Code:
def bossFight():
    global playerhealth
    global enemyhealth
    enemyhealth = 25.0
    playerhealth = 50.0
    bosshealth = 100.0
    global deadBoss
    deadBoss = False
    print("""

          The ghost is revealeed to you as Arnold!         
          """)
    print("The boss still has " + str(bosshealth) + " remaining!")
    print("You still have " + str(playerhealth) + " remaining! Good luck!")

    while playerhealth > 0 and bosshealth > 0:
        print("""

              Arnold glares at you. Do you choose to attack or defend?

              1: Attack
              2: Defend
              """)
        choiceBoss = int(input("Do you attack or defend?: "))
        
        if choiceBoss == 1:
            print("You attack the boss for 23.6 health!")
            bosshealth -= 23.6
            print("The boss has " + str(bosshealth) + " remaining!")
            print()
        elif choiceBoss == 2:
            print("The boss attacks you for 10.0 health!")
            playerhealth -= 10.0
            print("You have " + str(playerhealth) + " remaining!")
            print()

        if playerhealth <= 0:
            print("You have been defeated.")
            youLose()
        if bosshealth <= 0:
            deadBoss = True
            youWin()
        else:
            continue
                               

def hitBoss():                        
    print("You damage the boss!")
    bosshealth -= 10
    if bosshealth > 0:                        
        print("The boss still has " + str(bosshealth) + " remaining!")
    
    
def youLose():
    print("Nice try, " + name + ", try again next time!")

def youWin():
    print("Congratulations!")
    if deadBoss == False:
        print("You guessed the ghost, Arnold!")
    else:
        print("You defeated the ghost, Arnold!")
    print("Good job, " + name + "! Come back to the castle soon!")
Reply
#6
(Apr-10-2018, 05:44 PM)ecloev Wrote:
def youWin():
    print("Congratulations!")
    if deadBoss == False:

You're trying to use a variable that doesn't exist. If you're going to use a global variable, you have to let python know you're using a global variable.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  loop adventure game ilikedofs 1 1,671 May-26-2021, 12:43 AM
Last Post: bowlofred
  A text-based game [SOLVED] Gameri1 6 3,843 Apr-20-2021, 02:26 PM
Last Post: buran
  how to make a hotkey for text adventure game myn2018 2 1,918 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Winning/Losing Message Error in Text based Game kdr87 2 2,927 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  Choose your own adventure game noahc2004 2 2,523 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Simple cards game blackpanda 3 4,183 Apr-10-2020, 08:46 PM
Last Post: TomToad
  Simple text to binary python script gmills13 2 2,760 Feb-04-2020, 08:44 PM
Last Post: snippsat
  How best to format text in a simple text file? Pedroski55 2 3,140 Jun-14-2019, 06:07 AM
Last Post: noisefloor
  Looking for simple help - text file stripping DerekK 1 2,093 Mar-08-2019, 10:06 AM
Last Post: Larz60+
  Waiting in a text adventure StickyLizard 1 36,947 Jan-19-2019, 10:45 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020