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?
#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


Messages In This Thread
Errors in simple text adventure game? - by ecloev - Apr-06-2018, 05:12 PM
RE: Errors in simple text adventure game? - by ecloev - Apr-10-2018, 05:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  loop adventure game ilikedofs 1 1,756 May-26-2021, 12:43 AM
Last Post: bowlofred
  A text-based game [SOLVED] Gameri1 6 3,993 Apr-20-2021, 02:26 PM
Last Post: buran
  how to make a hotkey for text adventure game myn2018 2 2,032 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Winning/Losing Message Error in Text based Game kdr87 2 3,055 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  Choose your own adventure game noahc2004 2 2,637 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Simple cards game blackpanda 3 4,318 Apr-10-2020, 08:46 PM
Last Post: TomToad
  Simple text to binary python script gmills13 2 2,866 Feb-04-2020, 08:44 PM
Last Post: snippsat
  How best to format text in a simple text file? Pedroski55 2 3,250 Jun-14-2019, 06:07 AM
Last Post: noisefloor
  Looking for simple help - text file stripping DerekK 1 2,182 Mar-08-2019, 10:06 AM
Last Post: Larz60+
  Waiting in a text adventure StickyLizard 1 49,278 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