0

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")
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
user2633836
  • 131
  • 3
  • 5
  • 13

4 Answers4

3

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.

sihrc
  • 2,728
  • 2
  • 22
  • 43
  • +1 for noticing that the while condition is wrong in the asker's code. Might want to mention that explicitly though – StephenTG Aug 01 '13 at 17:05
  • That is great thank you. Didnt think of doing it that way I am new to Python so starting to pick up new methods. You have taught me how to do an array too I beleive thank you. what does the in function do please? – user2633836 Aug 01 '13 at 17:23
  • It checks if the value at rollAgain is somewhere in the list. returns a boolean – sihrc Aug 01 '13 at 19:43
1

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.

Stephan
  • 16,509
  • 7
  • 35
  • 61
  • That is fantastic thank you very much Stephan for clearing that up for me. I'm new to Python so the methods I have learnt so far is what I have been using to attempt to solve problems however what you have explained is of real assistance. – user2633836 Aug 01 '13 at 18:04
0
diceChoice = None
while diceChoice not in ["4","12","6"]:
    diceChoice = input("enter choice of dice(4,6,12)")
print "You picked %d"%diceChoice
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

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!")