0

I'm writing a simple 8ball response program and have an issue. When I run this program but give any option other than "y" or "yes" in response to the variable 'rd', the program thinks I have actually inputted "yes" and goes ahead with the code indented in the 'if' statement with a yes response. Why is this? I can't work out why.

import time
import random
import sys


resp = ["Yes!", "No!", "Maybe!", "Don't be so silly!",
        "In your dreams!", "Without a doubt!", "Most likely!",
        "Very doubtful!", "I'm going to have to say no this time!",
        "What kind of a question is that? Of course not!", "I reckon there's a 20% chance!",
        "Better not tell you now", "I've been told by to tell you no...",
        "I've been told to tell you yes...", "It's possible!", "More likely to see pigs fly!",
        "You wish!", "All signs point to no!", "All signs point to yes!",
        "If you truly believe it!"
    ]

def intro():
    print "Hello! Welcome to 8 Ball!\n"
    time.sleep(2)

def main():
    quit = 0
    while quit != "n":
        rd = raw_input("Are you ready to play? Enter y/n: ")
        if rd.lower() == "y" or "yes":
            question = raw_input("\nType your question and please press enter: ")
            print "\n"
            print random.choice(resp)
            print "\n"
            quit = raw_input("Do you want to roll again? Enter y/n: ")
        elif rd.lower() == "n" or "no":
            print "Looks like you need some more time to think. Have a few seconds to think about it.."
            time.sleep(3)
            quit = raw_input("Are you ready to play now? Enter y/n: ")
        else:
            print "That wasn't an option. Try again."
            rd = raw_input("Are you ready to play? Enter y/n: ") 
    print "Okay! Thanks for playing."

intro()
main()
spectras
  • 13,105
  • 2
  • 31
  • 53

3 Answers3

1
>>> bool("yes")
True

"yes" evaluates to true

if rd.lower() in ("y", "yes"):

could be used check to see if the value is 'y' or 'yes'

dm03514
  • 54,664
  • 18
  • 108
  • 145
0

You can't do this:

if rd.lower() == "y" or "yes":

because it's evaluating "yes" by itself. Instead try:

if rd.lower() == "y" or rd.lower() == "yes":

also consider:

if rd.lower() in ["y", "yes"]:
ergonaut
  • 6,929
  • 1
  • 17
  • 47
  • Thank you! I had been getting this issue in other programs I've written too and this solves a lot of problems. Much appreciated, have a nice day. – S. Python Nov 13 '15 at 17:05
0

You can't do if x == a or b in python you have to do x == a or x == b or x in (a, b) import time import random import sys

resp = ["Yes!", "No!", "Maybe!", "Don't be so silly!",
        "In your dreams!", "Without a doubt!", "Most likely!",
        "Very doubtful!", "I'm going to have to say no this time!",
        "What kind of a question is that? Of course not!", "I reckon there's a 20% chance!",
        "Better not tell you now", "I've been told by to tell you no...",
        "I've been told to tell you yes...", "It's possible!", "More likely to see pigs fly!",
        "You wish!", "All signs point to no!", "All signs point to yes!",
        "If you truly believe it!"
    ]

def intro():
    print "Hello! Welcome to 8 Ball!\n"
    time.sleep(2)

def main():
    quit = 0
    while quit != "n":
        rd = raw_input("Are you ready to play? Enter y/n: ")
        if rd.lower() in ("y", "yes"):
            question = raw_input("\nType your question and please press enter: ")
            print "\n"
            print random.choice(resp)
            print "\n"
            quit = raw_input("Do you want to roll again? Enter y/n: ")
        elif rd.lower() in ("n", "no"):
            print "Looks like you need some more time to think. Have a few seconds to think about it.."
            time.sleep(3)
            quit = raw_input("Are you ready to play now? Enter y/n: ")
        else:
            print "That wasn't an option. Try again."
            rd = raw_input("Are you ready to play? Enter y/n: ") 
    print "Okay! Thanks for playing."

intro()
main()