Mar-11-2017, 10:28 PM
You can set while True and a1 == 'yes'(string) else will be 'no',and break out on both.
But you soon get into trouble with this design.
Keeping all in functions make it easier to to jump around like playing a game and back to menu.
Eg:
You can build on game/menu function over jump over to other function,
can always fall back to menu where there is a way out(Quit).
With function you can build quite complex structure and your code don't get messy.
Using class is and other way to structure code,not needed for small games for larger games it can be useful
But you soon get into trouble with this design.
Keeping all in functions make it easier to to jump around like playing a game and back to menu.
Eg:
from six.moves import input import time def my_game(): print('Game running') for i in reversed(range(5)): time.sleep(1) print('Playing {}'.format(i)) input('Push enter to retun to menu') def menu(): while True: print('(1) Play game') print('(Q) Quit') choice = input('Enter your choice: ').lower() if choice == '1': my_game() elif choice == 'q': return False else: print('Not a correct choice: {}'.format(choice)) if __name__ == '__main__': menu()So here always fall back into menu,from here can Quit game or play new game.
You can build on game/menu function over jump over to other function,
can always fall back to menu where there is a way out(Quit).
With function you can build quite complex structure and your code don't get messy.
Using class is and other way to structure code,not needed for small games for larger games it can be useful