2015-11-13 51 views
0

我正在写一个简单的8ball响应程序并出现问题。当我运行这个程序但给出了除“y”或“yes”以外的任何选项来响应变量“rd”时,程序认为我实际上输入了“yes”并继续执行在'if'语句中缩进的代码回答是肯定的。为什么是这样?我无法弄清楚为什么。如果/ elif语句不能在while循环中工作

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

回答

1
>>> bool("yes") 
True 

“是” 计算结果为真

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

可以用来检查是否值是'y''yes'

0

你不能做到这一点:

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

,因为它本身评估为“是”。相反,尝试:

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

也考虑:

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

谢谢!我在其他程序中也遇到过这个问题,这解决了很多问题。非常感谢,祝你有美好的一天。 –

0

你不能做if x == a or b在Python中,你所要做的x == a or x == bx in (a, b) 进口时间 进口随机 进口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()