Python game loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python game loop

I've made a simple dice game which works perfectly but i'm trying to give the user the option to play again, the issue i have is when the user types "Y" it prints the original statement and i want to bypass it and just play again, i've included my code to make it easier to understand: from random import randint def game(): dice = randint(2, 12) start_game = False name = input('What is your name? ') win = 0 lose = 0 while start_game is False: print('Hello', name, 'welcome to the dice game, ' 'you have 2 dice to roll, 7 or higher you win, 6 or lower you lose, do you want to play?') answer = str.upper(input('Y/N: ')) if answer == 'Y': start_game = True if dice < 7: print('You rolled', dice, 'you lose!!') lose += 1 print('Wins: ', win, '\nLosses: ', lose) again = input('Want to play again? Y/N: ').upper() if again == 'N': exit() elif again == 'Y': start_game = False else: print('You rolled', dice, 'you win!!') win += 1 print('Wins: ', win, '\nLosses: ', lose) again = input('Want to play again? Y/N: ').upper() if again == 'N': exit() elif again == 'Y': start_game = False elif answer == 'N': print('Bye!') exit() else: print('Pick again:') if __name__ == '__main__': game()

16th Apr 2019, 7:56 PM
Nathan Bone
Nathan Bone - avatar
4 Answers
+ 1
Thanks for tidying my code up and thanks for answering my question I'm pretty new to python and programming in general
16th Apr 2019, 8:28 PM
Nathan Bone
Nathan Bone - avatar
+ 1
In the course of python that I'm following right now I don't learned yet some functions you used, like randint() and str.upper(), I understood their meaning from your code. So thanks for teach me something new in Python!
17th Apr 2019, 12:08 AM
Fab Pitt (Extro)
Fab Pitt (Extro) - avatar
0
I'm also new in Python, nevertheless I'd like to simplify things. So I think: the information about how works the game is better to show just in the first esecution. Next, in my opinion you have too much "if" nested for no reason. I tried to make eisier your code in this way: from random import randint def game(): dice = randint(2, 12) start_game = False name = input('What is your name? ') win = 0 lose = 0 print('Hello', name, 'welcome to the dice game, ' 'you have 2 dice to roll, 7 or higher you win, 6 or lower you lose.') while start_game is False: print('Do you want to play?') answer = str.upper(input('Y/N: ')) if answer == 'Y': start_game = True if dice < 7: print('You rolled', dice, 'you lose!!') lose += 1 print('Wins: ', win, '\nLosses: ', lose) start_game = False else: print('You rolled', dice, 'you win!!') win += 1 print('Wins: ', win, '\nLosses: ', lose) again = input('Want to play again? Y/N: ').upper() if again == 'N': exit() elif again == 'Y': start_game = False elif answer == 'N': print('Bye!') exit() else: print('Pick again:') if __name__ == '__main__': game() Sorry about my english, I'd like to write in english without use a online translator.
16th Apr 2019, 11:53 PM
Fab Pitt (Extro)
Fab Pitt (Extro) - avatar