if / elif语句不在while循环

时间:2015-11-13 16:58:57

标签: python

我正在编写一个简单的8ball响应程序并遇到问题。当我运行这个程序但是给出除了" y"之外的任何选项。或"是"为了响应变量',该程序认为我实际上输入了"是"并继续使用' if'中缩进的代码。声明回答是肯定的。为什么是这样?我无法解决原因。

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()

3 个答案:

答案 0 :(得分:1)

>>> bool("yes")
True

"是"评估为真

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

可以使用

检查以查看值是'y'还是'yes'

答案 1 :(得分:0)

你不能这样做:

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

因为它本身正在评估“是”。而是尝试:

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

还要考虑:

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

答案 2 :(得分:0)

你不能在python中if x == a or bx == a or x == bx in (a, b)     进口时间     随机导入     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()