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!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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:
1
2
3
4
5
6
7
    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:
1
2
3
4
5
6
7
    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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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:
1
2
3
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 2,240 May-26-2021, 12:43 AM
Last Post: bowlofred
  A text-based game [SOLVED] Gameri1 6 5,345 Apr-20-2021, 02:26 PM
Last Post: buran
  how to make a hotkey for text adventure game myn2018 2 2,676 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Winning/Losing Message Error in Text based Game kdr87 2 3,903 Dec-14-2020, 12:25 AM
Last Post: bowlofred
  Choose your own adventure game noahc2004 2 3,454 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Simple cards game blackpanda 3 5,428 Apr-10-2020, 08:46 PM
Last Post: TomToad
  Simple text to binary python script gmills13 2 4,223 Feb-04-2020, 08:44 PM
Last Post: snippsat
  How best to format text in a simple text file? Pedroski55 2 4,398 Jun-14-2019, 06:07 AM
Last Post: noisefloor
  Looking for simple help - text file stripping DerekK 1 2,721 Mar-08-2019, 10:06 AM
Last Post: Larz60+
  Waiting in a text adventure StickyLizard 1 57,781 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