1

My code is asking the user if they want a 4, 6 or 12 sided die to be rolled and if they don't put in a 4, 6 or 12, they should be asked the question again until they enter it correctly. It just keeps saying everything is invalid at the moment. Here is my current code:

number = int(input("Would you like to use a 4, 6 or 12 sided die\n"))
while number != 4 or number != 6 or number != 12:
    print("Invalid response")
    int(input("Would you like to use a 4, 6 or 12 sided die?\n"))
if number == 4:
    import random
    random4 = random.randint(1, 4)
    print("Your 4 sided die rolled a" ,random4)

elif number == 6:
    import random
    random6 = random.randint(1, 6)
    print("Your 6 sided die rolled a" ,random6)

elif number == 12:
    import random
    random12 = random.randint(1, 12)
    print("Your 12 sided die rolled a" ,random12)
acurtis869
  • 13
  • 2
  • your if is not part of the while block. – munk Jul 10 '14 at 16:16
  • 7 answers in less than 5 minutes would suggested that you need to RTFM. – devnull Jul 10 '14 at 16:21
  • Think about it; if you ask if number is not 4 **or** the number is not 6 **or** the number is not 12, then if the number is 4, two of those three conditions are met. – Martijn Pieters Jul 10 '14 at 16:23
  • I would recommend reading [this question](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) on good patterns for handling using input. Repeating the question line twice isn't the best way. – DSM Jul 10 '14 at 16:26
  • Please move the `import random` on top of your code. There's no need to repeat it over and over. – Matthias Jul 10 '14 at 17:13

7 Answers7

3

Your logic flow is a little bit off. You're asking for the number outside of the while loop, so the number never gets updated, and the while keeps evaluating the original value. You need to set the variable number to the result of the new input call.

while number != 4 or number != 6 or number != 12:
    number = int(input("Would you like to use a 4, 6 or 12 sided die\n"))

Edit: Some other responses touched on another reason this won't work quite right as well. You need to check that ALL of those things are true before saying it's not valid, not that ANY of them are true. A number can't be 4, 6 and 12 at the same time. So use and, or better yet:

while number not in [4, 6, 12]:
Carl
  • 905
  • 5
  • 9
2

To simplify your condition, try this:

while number not in (4, 6, 12):
...

Your condition is incorrect as no matter what, one of your checks will be true.

bedwyr
  • 5,774
  • 4
  • 31
  • 49
0

You need to understand how the or works:

Your code will come out of this loop:

while number != 4 or number != 6 or number != 12:

as soon as one of the conditions is true. Suppose you enter 6, the first condition, number != 4 is still true, so the control never gets to evaluating number != 6.

You need to be using and.

shaktimaan
  • 11,962
  • 2
  • 29
  • 33
0

you need to be using and instead of or. Your first line says "check if user didn't roll 4; then check if they didn't roll 6, then check if they didn't roll 12", except it is doing all of these separately. Because of this, rolling a 4 will still not be a 6 and not be a 12; you need to be using and where you are using or in the first line

bob12528
  • 1
  • 2
0

You need to be using and instead of or. Let's walk through what will happen one step at a time.

while number != 4 or number != 6 or number != 12:

First, you input a 4. The program walks down the condition one step at a time.

number != 4

This is false, since the number is, in fact, 4. Next:

number != 6

This turns out to be true, since we gave 4 and not 6. This means that the program heads right into the next line of code, asking for another roll. If you want a "none of these are true" kind of condition, you need to use and instead of or.

TheSoundDefense
  • 6,753
  • 1
  • 30
  • 42
0

Bushmills is suggesting you could just use and. Here is another approach.

from random import randrange
number = -1

while (number not in (4,6,12)):
  try:
    number = int(input("Would you like to use a 4, 6 or 12 sided die\n"))
  except ValueError:
    continue

print("Your {} sided died rolled a {}.".format(number,1+randrange(number)))
Alan
  • 9,410
  • 15
  • 20
0

Here's an edit that keeps your original structure. As a beginner, don't hesitate to write simple, straightforward code as you did. Your primary effort should be getting the program to work. You can always go back and refactor/rewrite code using the latest concepts you've learned. That's how it's done at all skill levels.

In this case, using while True: eliminates the compound OR statement. You could use either OR's or AND's with == and != respectively. Also, when using a while loop, you should initialize the checked value - number - outside the loop with a passing value, i.e. one that allows entry to the loop.

number = 0
while number != 4:
    #do stuff
    number = someNewValue

A couple of things you need to understand when using compound AND/OR conditions are short circuiting and operator precedence.

import random
while True: # loops until break
    number = int(input("Would you like to use a 4, 6 or 12 sided die?\n"))
    if number == 4:
        import random
        random4 = random.randint(1, 4)
        print("Your 4 sided die rolled a" ,random4)
        break
    elif number == 6:
        import random
        random6 = random.randint(1, 6)
        print("Your 6 sided die rolled a" ,random6)
        break
    elif number == 12:
        import random
        random12 = random.randint(1, 12)
        print("Your 12 sided die rolled a" ,random12)
        break

Once it's working, several ideas from the other posts should be applied. Other ideas are to make this a function and make the list of die sizes a variable.

Final code might look something like this.

from random import randrange

DIE_CHOICES = {4, 6, 12}

def roll(choices):
    while True:
        try:
            choice = int(input('Would you like to use a 4, 6 or 12 sided die?\n'))
        except ValueError:
            continue
        if roll in choices:
            return choice, 1 + randrange(choice)
        else:
            print 'Please pick a legal die size.\n'

print 'Your {} sided die rolled a {}.'.format(roll(DIE_CHOICES))

This makes things a little more generic and eliminates duplicate/redundant code.

ScottO
  • 129
  • 4