Python Forum
Alien game code from book - 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: Alien game code from book (/thread-12468.html)



Alien game code from book - TheMusicMan - Aug-26-2018

Im still on my book and got stuck at this code.


alien_0 = {'x_position': 0, 'y_poaition': 25, 'speed': 'medium'}
print('original x-position: ' + str(alien_o['x_position']))

#Move the alien to the right.
#Determine how far to move the alien based on its current speed.
if alien_0['speed'] =='slow':
	x_increment = 1
 elif alien_0['speed'] == 'medium':
	 x_increment = 2
 else:
	 # This must be a fast alien
	 x_increment = 3
 
 
# The new position is the old position plus the increment.
alien_0['x_position'] = alien_0['x_position'] + x_increment

print('new x_position: ' +str(alien_0['x_position']))
Comes up with this
[Image: cQ0pua9.png]

I tried moving the elif and else more left or right but it just changes the error then.
Any ideas i'm still newish at this but having trouble with making sure the (if, elif and else) are in the right places.


RE: Alien game code from book - buran - Aug-26-2018

Lines 7-12 need to move one space to the left


RE: Alien game code from book - TheMusicMan - Aug-26-2018

I might be doing it wrong but its still saying the same error


RE: Alien game code from book - buran - Aug-26-2018

Sorry, my bad. It was just lines 8-12. line 7 was OK. Note also there is misspelled variable name on line 2

alien_0 = {'x_position': 0, 'y_poaition': 25, 'speed': 'medium'}
print('original x-position: ' + str(alien_0['x_position']))
 
#Move the alien to the right.
#Determine how far to move the alien based on its current speed.
if alien_0['speed'] =='slow':
    x_increment = 1
elif alien_0['speed'] == 'medium':
    x_increment = 2
else:
    # This must be a fast alien
    x_increment = 3
  
  
# The new position is the old position plus the increment.
alien_0['x_position'] = alien_0['x_position'] + x_increment
 
print('new x_position: ' +str(alien_0['x_position']))



RE: Alien game code from book - TheMusicMan - Aug-26-2018

Ok got that. Thanks for the help.