Reputation: 141
I am trying to create a dice game using a while loop and if's. I have done this successfully however I am trying to figure out how to program the game so that if numbers 4,6 or 12 are not input it will state invalid choice and will ask diceChoice again. Can anybody help?
So far I have...
rollAgain = "Yes" or "yes" or "y"
while rollAgain == "Yes" or "yes" or "y":
diceChoice = input ("Which dice would you like to roll; 4 sided, 6, sided or 12 sided?")
if diceChoice == "4":
import random
print("You rolled a ", random.randint(1,4))
if diceChoice == "6":
import random
print("You rolled a ", random.randint(1,6))
if diceChoice == "12":
import random
print("You rolled a ", random.randint(1,12))
rollAgain = input ("Roll Again?")
print ("Thank you for playing")
Upvotes: 0
Views: 13433
Reputation: 17991
You should only import random once at the top
import random #put this as the first line
Your rollAgain declaration should only set it to one value
rollAgain = "yes" # the or statements were not necessary
You forgot to do rollAgain ==
in your subsequent conditionals, here is a simpler way
while rollAgain.lower().startswith("y"): #makes sure it starts with y or Y
To do an invalid input statement, you could use elif:
and else:
statements to keep it simple
if diceChoice == "4":
print("You rolled a ", random.randint(1,4))
elif diceChoice == "6":
print("You rolled a ", random.randint(1,6))
elif diceChoice == "12":
print("You rolled a ", random.randint(1,12))
else:
print("Invalid Input, please enter either 4, 6, or 12")
Your old while loop would never have exit because you were basically saying this
while rollAgain == "Yes" or True or True #because non-empty strings are treated as True
edit since you asked about in
statements, here is a brief example
>>>5 in [1,2,3,4]
False
>>>5 in [1,2,3,4,5]
True
The in
statement is similar to a contains()
in other languages it checks to see if the variable is inside the list
Since 5
is not in the list [1,2,3,4]
it returned False
however, 5
IS in the list [1,2,3,4,5]
so it returns True
You could use this several places in your code, specifically if you want to make sure a variable is within a set of options. I didn't recommend it to keep it simple for you.
Upvotes: 1
Reputation:
Just my take on it:
# If you are only using one function from random
# then it seems cleaner to just import that name
from random import randint
while True:
choice = int(input("Which dice would you like to roll; 4 sided, 6, sided or 12 sided?\n:"))
# Using sets for 'in' comparisons is faster
if choice in {4, 6, 12}:
print("You rolled a", randint(1, choice))
else:
print("Please input 4, 6, or 12.")
# Make the input lowercase so that it will accept anything that can be
# interpreted as "yes".
if input("Roll Again?\n:").lower() not in {"yes", "y"}:
# End the game by breaking the loop
break
# You should use an input at the end to keep the window open to see the results
input("Thank you for playing!")
Upvotes: 0
Reputation: 2848
Fixed While Loop, Tidied up all the repetition. Moved import statement to the top. Structured to allow more options for rollAgain and diceChoice.
import random
rollAgain = "Yes"
while rollAgain in ["Yes" , "yes", "y"]:
diceChoice = input ("Which dice would you like to roll; 4 sided, 6, sided or 12 sided?")
if diceChoice in ["4","6","12"]:
print("You rolled a ",random.randint(1,int(diceChoice)))
else:
print "Please input 4,6, or 12."
rollAgain = input ("Roll Again?")
print ("Thank you for playing")
Doing this sort of assignment:
rollAgain = "Yes" or "yes" or "y"
Is unnecessary - only the first value will be inputted. Pick one for this variable; you only need one for its purposes.
This sort of assignment doesn't work here either:
while rollAgain == "Yes" or "yes" or "y":
It will again only check the first value. You either have to split it up like other posters have done, or use a different data structure that will incorporate them all like a list in the code above.
Upvotes: 3
Reputation: 114038
diceChoice = None
while diceChoice not in ["4","12","6"]:
diceChoice = input("enter choice of dice(4,6,12)")
print "You picked %d"%diceChoice
Upvotes: 0