Python Forum
help with while loop on dice game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with while loop on dice game
#1
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
Reply
#2
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.
Reply
#3
(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!")
...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,077 Dec-14-2021, 12:23 AM
Last Post: supuflounder
  Need some help with Dice Game for daughter in school OptX 2 1,895 Feb-12-2021, 08:43 PM
Last Post: BashBedlam
  regarding dice game hola123 4 2,290 Feb-10-2021, 11:00 PM
Last Post: hola123
  Two dice Game of Pig help elliemehl 2 4,167 Feb-14-2019, 01:19 AM
Last Post: woooee
  Two Dice Pig Game in Python 3.6 Help inspired22 4 11,537 Oct-10-2018, 01:17 PM
Last Post: ichabod801
  dice game im stuck sylerr 3 4,388 May-12-2017, 10:50 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020