Apr-10-2018, 05:44 PM
(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!")