#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Confused ("else:" being invalid syntax within a "while:" loop) [CLOSED]


    So I'm very new to python. Only started learning it properly six days ago as my first programming language.

    At the moment I'm testing my knowledge with a text game I've named "bearbattle.py" and while testing it, I've found something peculiar.

    I have the same "while:" loop for two different scenarios within my game so far, but one of the "else:" works, and the other comes up as invalid syntax when I try to run it.

    Debugging doesn't help me find the reason for it.

    Something to note is that I use the program "Geany" to type the code, using the tab key for my indentation.

    Thank you.

    Here is the code:

    The problem lies on line 229.

    Okay, great. I can't share the code in a URL or downloadable file so I'll have to copy and paste it here. Oh well, here it is (no indentation, yay - I'm sure you know where the indentation is):



    This is line 229 and onwards:



    Code:
    		else:
    			print ('Enter a command. If you can\'t remember the commands, type "commands".')
    			print ()
    	
    	#Bear  vulnerable, not able to attack
    	#while scenario == 3:
    		
    	
    	#Bite attack
    	#while scenario == 4:
    		
    	
    	#Player vulnerable, not able to attack
    	#while scenario == 5:





    This is the "while:":



    Code:
    	#Takedown ability
    	while scenario == 2:
    		
    		print ('The bear raises itself on its hind legs in preparation to attack.')
    		print ('What will you do?')
    		print ()
    		
    		direction = input()
    		
    		if direction == 'commands':
    			listCommands()
    		
    		elif direction == 'stats':
    			listStats(health, ability, strength, defence, bearHealth, bearAbility, bearStrength, bearDefence)
    		
    		elif direction == 'inventory':
    			print (inventory[0] + ' - ' + items[inventory[0]])
    			print ()
    		
    		elif 'use' in direction:
    			if 'health potion' in direction:
    				if 'health potion' in inventory:
    					if health < 100:
    						print ('You restore 20 health.')
    						health = health + 20
    						inventory.remove('health potion')
    						if health > 100:
    							health = 100
    						print ('You are now on ' + str(health) + ' health.')
    						print ()
    						print ('The bear uses "Takedown"!')
    						print ('The bear comes down on you, claws first. -40 health (bear strength * 2), -20 bear ability points. You are knocked to the floor, and vulnerable to the bear, unable to attack.')
    						health = health - 40
    						bearAbility = bearAbility - 20
    						turns = turns + 1
    						scenario = 5
    						time.sleep(3)
    						print ()
    						print ('TURN ' + str(turns) + ':')
    					else:
    						print ('You are already full health (100).')
    				else:
    					print ('Use an item from your inventory or an ability from your abilities list.')
    			else:
    				print ('Use an item from your inventory or an ability from your abilities list.')
    		
    		elif direction == 'attack':
    			print ('You leap towards the bear\'s exposed stomach, doing additional damage! This attack also knocks the bear to its side, vulnerable to another attack. -40 bear health (your strength * 2).')
    			bearHealth = bearHealth - 40
    			turns = turns + 1
    			scenario = 3
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':')
    		
    		elif direction == 'defend':
    			print ('The bear uses "Takedown"!')
    			print ('Being unarmed, your defense is destroyed by the bear, causing the action to have no effect whatsoever. -40 health (bear strength * 2), -20 bear ability points. At least you are ready to attack again.')
    			health = health - 40
    			bearAbility = bearAbility - 20
    			turns = turns + 1
    			scenario = 1 or 4
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':')
    		
    		elif direction == 'dodge':
    			print ('The bear uses "Takedown"!')
    			print ('You easily dodge the slow attack, causing the bear to fall flat on its face. The bear is now vulnerable to attack! -20 bear ability points.')
    			bearAbility = bearAbility - 20
    			turns = turns + 1
    			scenario = 3
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':'
    		
    		else:
    			print ('Enter a command. If you can\'t remember the commands, type "commands".')
    			print ()


    This is the whole code:




    Code:
    import time
    
    on = 1
    off = 2
    game = on
    turns = 1
    scenario = 1
    
    health = 100
    ability = 100
    strength = 10
    
    bearHealth = 200
    bearAbility = 50
    bearStrength = 20
    
    inventory = ['health potion']
    items = {'health potion':'restores 20 health'}
    
    def listCommands():
    	print ('Use the "attack" command to attack the bear.')
    	print ('Use the "defend" command to defend from the bears attack.')
    	print ('Use the "dodge" command to dodge the bears attack.')
    	#print ('Use the "special" command to view you abilities.')
    	print ('Use the "stats" command to view your statistics and the bears statistics.')
    	print ('Use the "inventory" command to view your items.')
    	print ('Use the "use" command to use an ability or an item in your inventory')
    	print ('Use the "commands" command to view the list of commands again.')
    	print ()
    
    def listStats(health, ability, strength,  bearHealth, bearAbility, bearStrength):
    	health = str(health)
    	ability = str(ability)
    	strength = str(strength)
    	
    	bearHealth = str(bearHealth)
    	bearAbility = str(bearAbility)
    	bearStrength = str(bearStrength)
    	
    	print ()
    	print ('Your Health: ' + health)
    	print ('Your Ability Points: ' + ability)
    	print ('Your Strength: ' + strength)
    	print ()
    	print ('Bear\'s Health: ' + bearHealth)
    	print ('Bear\'s Ability Points: ' + bearAbility)
    	print ('Bear\'s Strength: ' + bearStrength)
    	print ()
    
    print ('B E A R   C O M B A T')
    print ()
    
    time.sleep(1)
    
    listCommands()
    
    time.sleep(5)
    
    print ('You are encountered by an angry bear in the forest!')
    print ('Using your limited knowledge of unarmed combat, you engage the bear in battle.')
    print ()
    
    time.sleep(3)
    
    print ('TURN 1:')
    
    while game == on:
    	
    	#Claw attack
    	while scenario == 1:
    		
    		print ('The bear poses in front of you, about to lunge forwards to attack you with its claws.')
    		print ('What will you do?')
    		print ()
    		
    		direction = input()
    		
    		if direction == 'commands':
    			listCommands()
    		
    		elif direction == 'stats':
    			listStats(health, ability, strength, defence, bearHealth, bearAbility, bearStrength, bearDefence)
    		
    		elif direction == 'inventory':
    			print (inventory[0] + ' - ' + items[inventory[0]])
    			print ()
    		
    		elif 'use' in direction:
    			if 'health potion' in direction:
    				if 'health potion' in inventory:
    					if health < 100:
    						print ('You restore 20 health.')
    						health = health + 20
    						inventory.remove('health potion')
    						if health > 100:
    							health = 100
    						print ('You are now on ' + str(health) + ' health.')
    						print ()
    						print ('The bear attacks you with its claws. -20 health (bear strength).')
    						health = health - 20
    						turns = turns + 1
    						if bearAbility > 19:
    							scenario = 2
    						else:
    							scenario = 4
    						time.sleep(3)
    						print ()
    						print ('TURN ' + str(turns) + ':')
    					else:
    						print ('You are already full health (100).')
    				else:
    					print ('Use an item from your inventory or an ability from your abilities list.')
    			else:
    				print ('Use an item from your inventory or an ability from your abilities list.')
    		
    		elif direction == 'attack':
    			print ('You attack the bear, hitting it just as it hits you. -20 health (bear strength), -10 bear health (your strength). You are both equally delayed to return to a ready posistion.')
    			health = health - 20
    			bearHealth = bearHealth - 10
    			turns = turns + 1
    			if bearAbility > 19:
    				scenario = 2
    			else:
    				scenario = 4
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':')
    		
    		elif direction == 'defend':
    			print ('You attempt to block the bears claws, but without any equipment it easily cuts through your feeble hand block. You at least managed to defend against some of the damage. -10 health (bear strength / 2).')
    			health = health - 10
    			turns = turns + 1
    			if bearAbility > 19:
    				scenario = 2
    			else:
    				scenario = 4
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':')
    		
    		elif direction == 'dodge':
    			print ('You dodge the bears attack, deciding it would be better to dodge the slow attack instead of attempt to block it. The bear is now vulnerable to attack!')
    			turns = turns + 1
    			scenario = 3
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':')
    		
    		else:
    			print ('Enter a command. If you can\'t remember the commands, type "commands".')
    			print ()
    	
    	#Takedown ability
    	while scenario == 2:
    		
    		print ('The bear raises itself on its hind legs in preparation to attack.')
    		print ('What will you do?')
    		print ()
    		
    		direction = input()
    		
    		if direction == 'commands':
    			listCommands()
    		
    		elif direction == 'stats':
    			listStats(health, ability, strength, defence, bearHealth, bearAbility, bearStrength, bearDefence)
    		
    		elif direction == 'inventory':
    			print (inventory[0] + ' - ' + items[inventory[0]])
    			print ()
    		
    		elif 'use' in direction:
    			if 'health potion' in direction:
    				if 'health potion' in inventory:
    					if health < 100:
    						print ('You restore 20 health.')
    						health = health + 20
    						inventory.remove('health potion')
    						if health > 100:
    							health = 100
    						print ('You are now on ' + str(health) + ' health.')
    						print ()
    						print ('The bear uses "Takedown"!')
    						print ('The bear comes down on you, claws first. -40 health (bear strength * 2), -20 bear ability points. You are knocked to the floor, and vulnerable to the bear, unable to attack.')
    						health = health - 40
    						bearAbility = bearAbility - 20
    						turns = turns + 1
    						scenario = 5
    						time.sleep(3)
    						print ()
    						print ('TURN ' + str(turns) + ':')
    					else:
    						print ('You are already full health (100).')
    				else:
    					print ('Use an item from your inventory or an ability from your abilities list.')
    			else:
    				print ('Use an item from your inventory or an ability from your abilities list.')
    		
    		elif direction == 'attack':
    			print ('You leap towards the bear\'s exposed stomach, doing additional damage! This attack also knocks the bear to its side, vulnerable to another attack. -40 bear health (your strength * 2).')
    			bearHealth = bearHealth - 40
    			turns = turns + 1
    			scenario = 3
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':')
    		
    		elif direction == 'defend':
    			print ('The bear uses "Takedown"!')
    			print ('Being unarmed, your defense is destroyed by the bear, causing the action to have no effect whatsoever. -40 health (bear strength * 2), -20 bear ability points. At least you are ready to attack again.')
    			health = health - 40
    			bearAbility = bearAbility - 20
    			turns = turns + 1
    			scenario = 1 or 4
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':')
    		
    		elif direction == 'dodge':
    			print ('The bear uses "Takedown"!')
    			print ('You easily dodge the slow attack, causing the bear to fall flat on its face. The bear is now vulnerable to attack! -20 bear ability points.')
    			bearAbility = bearAbility - 20
    			turns = turns + 1
    			scenario = 3
    			time.sleep(3)
    			print ()
    			print ('TURN ' + str(turns) + ':'
    		
    		else:
    			print ('Enter a command. If you can\'t remember the commands, type "commands".')
    			print ()
    	
    	#Bear  vulnerable, not able to attack
    	#while scenario == 3:
    		
    	
    	#Bite attack
    	#while scenario == 4:
    		
    	
    	#Player vulnerable, not able to attack
    	#while scenario == 5:
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Edit your post to use code tags or it is unreadable.
    -Mek
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    No, we don't know how to indent the program. I'm special, so I'll help this once. Compilers detect syntax errors after the mistake in the program source. That's because the parser is busy looking ahead trying to make sense of your program in a way the programmer didn't expect---but is valid syntax. Please use the instructions at my signature to indent your future code posts.
    Code:
            elif direction == 'dodge':
                print ('The bear uses "Takedown"!')
                print ('You easily dodge the slow attack, causing the bear to fall flat on its face. The bear is now vulnerable to attack! -20 bear ability points.')
                bearAbility = bearAbility - 20
                turns = turns + 1
                scenario = 3
                time.sleep(3)
                print ()
                print ('TURN ' + str(turns) + ':'  ###########NEED RIGHT PARENTHESIS
            else:
                print ('Enter a command. If you can\'t remember the commands, type "commands".')
                print ()
    Last edited by b49P23TIvg; May 9th, 2013 at 08:20 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Sorry, and thanks for the help.

IMN logo majestic logo threadwatch logo seochat tools logo