-2

I'm trying to create a small python program to emulate rolling a single die. My problem arises in that I want the program to accept both upper and lower-case 'Y' to trigger a re-roll. My program works when I accept either upper or lower case y but when I try to change the line

if usr == 'Y'

to

if usr == 'Y' or 'y'

creates a problem where it never enters the else statement, making the program never terminate.

import random

def DiceRoll():
 number = random.randrange(1, 7, 1)
 print("Dice Roll: ", number)
 AskUser()
 return

def AskUser():
 usr = input("Roll Again? (Y/N) \n")
 if usr == 'Y':
     DiceRoll()
 else :
     print("Thank you for playing!")
return

DiceRoll()
Zac
  • 1
  • 2
  • 3
    try `if usr in ('Y', 'y')`, or `if usr.lower() == 'y'` – asongtoruin Jul 10 '17 at 16:15
  • just use usr = usr.lower() and only check for lowercase y – Jason V Jul 10 '17 at 16:17
  • I changed it to `if usr in ('Y','y'):` and it works now. Thank you! – Zac Jul 10 '17 at 16:20
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/148815/discussion-on-question-by-zac-python-if-statement-and-logical-operator-issue). – deceze Jul 10 '17 at 18:54

1 Answers1

1

You must do

if usr == 'Y' or usr == 'y'.

It is currently evaluating the statement 'y' as its own condition, and in this case 'y' always evaluates to true.

Jason S
  • 129
  • 3