为什么不循环?

时间:2014-10-02 03:06:32

标签: python python-2.7

Python 2.7

Windows 7

当我输入一次“播放”时:

if(game == "play"):
print("Make sure you type 'Play', CaPS MaTteR! ")
startg()
elif(game == "Play"):
playg()

它做它应该做的事情。当我输入“播放”错误两次时,而不是循环播放它只是退出。在Google上搜索但无法找到任何内容:/

代码:

name = raw_input("What is your name? ")
gender = raw_input("What are you, a Boy or a Girl? ")
print(" ")

if(gender == "Boy"):
their = "his "
else:
their = "her "

game = raw_input("Type 'Play' to start. ")

def endg():
print("Hope you had fun! ~Red")

def startg():
game = raw_input("Type 'Play' to start. ")
if(game == "Play"):
 print("Loading. . . ")
 playg()

listq1 = ["A. Quit your job." , "B. Pretend you never saw the stack of papers." , "C.             Kill yourself because you don't feel like playing this game. "]

def playg():
answer = raw_input("You are a programmer, " + name  + ", who hates " + their + "job     very much." +
      " You walk into work to see a huge stack of papers on your desk... What do you     do? \n" + "\n".join(listq1))
if(answer == "A"):
    print("\nYou look around the room and see the flock of miserable people...Your     Co-Workers.  Is working here really worth the stress? ")
elif(answer == "B"):
    print("\nYou pull the over-sized recycle bin out from under your desk.  Just as you start to slide the papers to their impending doom, a fellow co-worker stops to ask what you are doing. ")
elif(answer == "C"):
    endg()

if(game == "play"):
print("Make sure you type 'Play', CaPS MaTteR! ")
startg()
elif(game == "Play"):
playg()

1 个答案:

答案 0 :(得分:2)

看看你的代码:

def startg():
game = raw_input("Type 'Play' to start. ")
if(game == "Play"):
 print("Loading. . . ")
 playg()

if(game == "play"):
print("Make sure you type 'Play', CaPS MaTteR! ")
startg()
elif(game == "Play"):
playg()

如果你错误输入" Play"会发生什么? as" play"第一次?好吧,它转到startg()。条件if(game=="Play"),但没有其他声明来处理除正确输入之外的任何事情。所以,如果你输入" play"第二次,这导致程序不考虑的条件。因此,不会调用任何函数,并且您的程序将继续执行。

顺便说一下,你应该引入一个else语句来处理" Play"以其他方式拼错了。不正确地大写不是您应该准备处理的唯一潜在错误。