Python Forum

Full Version: help with while loop on dice game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have to program a dice game for my school homework and I am stuck on part of the program so would like so help please. The below code in Python should ask the users who wants to play user 1 or 2. It should then stay in the second while loop all the time the user enters "y" but it goes back to the first question when "y" is entered. What am I doing wrong.


while 1:
userTurn = input ("Enter player 1 or 2 : ")
while userTurn == 1:
userAnswer = raw_input ("Do you want to roll dice? Y or N : ")
if userAnswer == "Y" or "y":
print ("rolleing add random code")
if userAnswer == "N" or "n":
break
Thread moved to "Homework".
Please edit your post so that the code is included in Python code tags. Also use ctrl+shift+v when copying code, that will keep indentation and make your code readable. You can find help here.
(Dec-14-2017, 07:01 PM)sean5 Wrote: [ -> ]if userAnswer == "Y" or "y":

That's not going to do what you expect.  Here's a few examples of or, ending up with a few ways you should do this instead:
>>> value = "n"
>>> if value == "Y" or "y":
...   print("match!")
...
match!
>>> value == "Y" or "y"
'y'
>>> if 'y':
...   print("match!")
...
match!
>>> if value.lower() == "y":
...   print("match!")
...
>>> if value in ["Y", "y"]:
...   print("match!")
...