0

I'm creating a dice game and I'm trying to break out of this infinite loop. Fixed by putting str on input so again=str(input('Roll again?')

import random

def play_again():
    again = input('Would you like to roll again? ')
    if again == 'y' or 'Y':
        main()
    if again == 'n' or 'N':
        print('Exiting the game.')

def roll():
    print('Rolling the dice.')

def exit():
    print('Ending game.')

def result():
    print('The number is '+str(random.randint(1,6))+'.')

def main():
    roll()
    result()
    play_again()



main()

I know I could just put everything in one method, but I rather keep it the way it is. The program runs fine till it reaches play_again(). No matter what letter I press, it runs the program again. How would I fix it so it runs again if I press y and quit when I press n?

idkidk
  • 1
  • 1
  • Marking as duplicate doesn't exactly answer their question. The problem is `again == 'y' or 'Y'` is the same as `(again == 'y') or ('Y')`. The second value here always evaluates to true. – Cireo May 11 '18 at 23:52
  • When I remove the y and keep the n, it says exiting the game no matter which key I press. How would I fix it so it runs properly? – idkidk May 11 '18 at 23:56
  • I fixed it by putting str(input(____)) – idkidk May 11 '18 at 23:59
  • input returns `str`, so that is a red herring, you would need to do `again in ['y', 'Y']` or `again == 'y' or again == 'Y'` or similar – Cireo Aug 27 '18 at 01:07

0 Answers0