Errors in simple text adventure game? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Errors in simple text adventure game? (/thread-9405.html) |
Errors in simple text adventure game? - ecloev - Apr-06-2018 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() RE: Errors in simple text adventure game? - nilamo - Apr-06-2018 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.
RE: Errors in simple text adventure game? - ecloev - Apr-07-2018 (Apr-06-2018, 05:52 PM)nilamo Wrote: Could you copy/paste the entire traceback? So basically, if I move the except on line 108 up a bit and then fix the indentation I should be good? RE: Errors in simple text adventure game? - nilamo - Apr-07-2018 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. RE: Errors in simple text adventure game? - ecloev - Apr-10-2018 (Apr-07-2018, 11:03 PM)nilamo Wrote: No, because then you'd have a different try: that didn't have an except anymore. 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!") RE: Errors in simple text adventure game? - nilamo - Apr-10-2018 (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. |