1

For some reason my program keeps rolling the dice again regardless of the users answer, and it doesn't print the goodbye message.

I'll include the code below so you can test yourself (Python 3)

from random import randint

def rollthedice():
    print("The number you rolled was: "+ str(randint(1,6)))

rollthedice()

shouldwecontinue = input("Do you wish to roll again? (y or n) ").lower()

if shouldwecontinue == "y" or "yes":
    rollthedice()

elif shouldwecontinue == "n" or "no":
    print("Goodbye.")

elif shouldwecontinue != type(str):
    print("Sorry the program only accepts 'y' or 'n' as a response.")

The desired effect of the program should be that if the user enters y, it rolls the dice again, respectively starting the whole program again. Whereas, if the user enters no then it should print the Goodbye message.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
Joob Loob
  • 19
  • 3
  • 1
    We see this mistake almost every day on SO, it's very common. Use `if shouldwecontinue in ("y", "yes"):` instead. – Paul Rooney Dec 19 '16 at 23:45
  • 1
    @PaulRooney - Almost every day? More like multiple times per day. >.> – TigerhawkT3 Dec 20 '16 at 00:09
  • And, OP, you might consider a question title that couldn't technically apply to every single question on the site. If you were reading a manual's troubleshooting section, how helpful would it be if the table of contents had 20 identical entries labeled "when something goes wrong and you want to fix it"? – TigerhawkT3 Dec 20 '16 at 00:31

1 Answers1

3

or doesn't work like that. Right now your program is evaluating (shouldwecontinue == "y") or "yes", and since Python interprets "yes" as truthy, then that condition always succeeds. The simplest fix is to change it to shouldwecontinue == "y" or shouldwecontinue == "yes". And then similar changes for the other conditional expressions, of course.

user3030010
  • 1,767
  • 14
  • 19
  • Ah omg I can't believe I oversaw that, my bad. Thanks buddy! – Joob Loob Dec 19 '16 at 23:45
  • 1
    ...or more Pythonically `if shouldwecontinue in ("y", "yes"):` – Lee Daniel Crocker Dec 19 '16 at 23:45
  • @LeeDanielCrocker or even `if shouldwecontinue in {"y", "yes"}:`. And yeah, it doesn't *really* matter :-) – Reut Sharabani Dec 19 '16 at 23:49
  • @LeeDanielCrocker While more Pythonic, that's probably not the kind of thing someone who is still learning how an `if` statement works needs to be worrying about right now. I think it's better to focus on getting the basics of a language correct first before worrying about the 'right' way to do something. – user3030010 Dec 19 '16 at 23:54
  • If you find a common duplicate question, you can vote or flag it as such rather than answering. The asker will then get a much more complete answer via the duplicate link. – TigerhawkT3 Dec 20 '16 at 00:12