0

I'm sorry for the confusing title but I didn't really know what to put there. I'm a beginner at Python and I have run into a little problem. I put together a simple dice roller with the question if you want to play again or not. This works fine but I would like the program to ask the input again if answer by anything other than "y" or "n". For now I have this:

import random
print("Welcome to the dice game")
while True:
    n = random.randint(1, 6)
    print("Press Enter to roll the dice...")
    input()
    print(n)
    answer = input("Do you want to play again? y/n: ")
    if answer == "y":
        continue
    elif answer == "n":
        break
    else:
        while answer != "n" or "y":
            answer = input("Please input y or n: ")
print("Thanks for playing")

I'm guessing that I can't rename a variable that is outside a loop from the inside. Is it possible to return the code to the first input?

Thanks

Oddy36
  • 17
  • 1
  • 4
    `while answer != "n" or "y"` does not do what you probably think it does. – pault Jul 05 '18 at 22:07
  • 4
    Almost correct: while `answer!= 'y' or answer!='n'`. Or `answer not in 'yn'`. Otherwise good job with other pitfalls :) – Andras Deak -- Слава Україні Jul 05 '18 at 22:08
  • Classic case of order of evaluation – the_constant Jul 05 '18 at 22:19
  • @pault Isn't it "While the answer is not "y" or "n" continue the loop"? – Oddy36 Jul 05 '18 at 22:20
  • It is, in English. But you are [programming in Python](https://stackoverflow.com/q/20002503/2564301) here. (Even in English, I realize now, there could be ambiguity. If you mean "not 'y' **or** 'n'", 'q' would be valid, because it satisfies the first comparison: 'q' = 'not 'y''.) – Jongware Jul 05 '18 at 22:24
  • 2
    Read the three linked duplicates above your question, they'll help. – Andras Deak -- Слава Україні Jul 05 '18 at 22:29
  • If I understand correctly I should use: `while answer != "n" or answer != "y":` (i hope) but i checked the other links but didn't find anything about the loop repeating when i try to change the variable. I'm very new so reading code takes a while lol and I might not see it – Oddy36 Jul 05 '18 at 22:33
  • Because loops don't scope variables in python; the rest of your code should work. It kept looping because `or 'n'` evaluates to truthy. Fixing that should fix your program. – Andras Deak -- Слава Україні Jul 05 '18 at 22:38
  • I see where I am wrong but I can't seem to figure out how to make it work. Should I put the second `while` elsewhere so that the code would restart at line 8? – Oddy36 Jul 05 '18 at 22:41
  • Sorry, I made a mistake :( `answer !='y' and answer!='n'`. AND. Think it through yourself to be sure :) `answer not in ['y','n']` may be better (clearer). – Andras Deak -- Слава Україні Jul 05 '18 at 22:44
  • 1
    `answer != "n" or "y"` is equivalent to `(answer != "n") or "y"`. If `answer` does equal 'n' the whole expression evaluates to True, since `or` won't even look at the 2nd part, otherwise the whole expression evaluates to 'y'. So in either case the `while` test will pass because a non-empty string like 'y' is truthy. – PM 2Ring Jul 05 '18 at 22:55
  • 1
    Oh, `while answer != "y" and answer != "n":` did the trick. I then just added `if answer == "n": break` and now works perfectly. I got confused with the `and` & `or` Thanks a lot – Oddy36 Jul 05 '18 at 23:00
  • No problem, and welcome to python and Stack Overflow :) – Andras Deak -- Слава Україні Jul 05 '18 at 23:02

0 Answers0