Python 2.7.8 How to restart the game code -
i looking on how restart code without restarting game. want able have game restart on on.
print 'welcome game of nim: ' player=1 state=21 print 'the number of objects ',state while true: print 'player ',player while true: move=input('what move? ') if move in [1,2,3] , move<state: break print 'illegal move.' state=state-move print 'the number of objects ',state if state==1: print 'player ',player,' wins!' break if player==1: player=2 else: player=1 print 'game over.'
you can put code in function, , call function in while loop:
from sys import exit #to exit if user not want try again print 'welcome game of nim: ' def game(): player=1 state=21 print 'the number of objects ',state while true: print 'player ',player while true: move=input('what move? ') if move in [1,2,3] , move<state: break print 'illegal move.' state=state-move print 'the number of objects ',state if state==1: print 'player ',player,' wins!' break if player==1: player=2 else: player=1 if raw_input('would try again? ').lower().startswith('y'): return #to exit function cleanly sys.exit() #to exit program cleanly if user not want try again def run(): while true: game() if __name__ == '__main__': run()
Comments
Post a Comment