0

I'm programming a dice game, that uses the random function. When you run the program it rolls two dice once and asks the user if they want to roll the dice again. My main problem is that after the initial dice roll has been processed and the program asks the user if they want to roll again it causes an infinite loop that I don't know how to stop. How do I prevent an infinite loop in a while statement when the program is testing a string input? Thanks in advance.

import random

print ('Welcome to the DICE GAME!')

die1 = random.randint(1,6)
die_1 = random.randint(1,6)
    
print(die1, die_1)
print("Your initial score is: " + str(die1 + die_1))
yes_or_no = input(' Would you like to restart the dice game. Print "Y" or "y" to continue. Type anything else to exit. :')
print (yes_or_no)
while yes_or_no == 'y' or "Y":
    print(die1, die_1)
else:
    print('Thanks for playing the DICE GAME! Goodbye.')
  • Use `yes_or_no.lower() == 'y'`. – Samwise Sep 29 '22 at 21:23
  • You could put everything into a function "def rolldice()" and after the game is dice is rolled once, ask if they want to roll again. "rollagain = input("Play again? Y/N: ")" If "y" they can roll again, if "else", you can end the game. Also, search how to use .lower(), so you don't need to code for "Y" and "y" inputs. – pc510895 Sep 29 '22 at 22:42

0 Answers0