为什么不循环呢?

2024-09-28 05:17:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: thetoansweryougameinputplayyour
1条回答
网友
1楼 · 发布于 2024-09-28 05:17:52

看看你的代码:

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”误输入为“Play”,会发生什么情况?好吧,它去了startg()。有条件的if(game=="Play"),但是除了正确的输入,没有其他语句来处理任何事情。因此,如果您第二次键入“play”,则会导致程序无法解释的情况。所以没有函数被调用,程序一直进行到最后。你知道吗

顺便说一下,您应该引入else语句来处理“Play”以其他方式拼写错误的所有情况。不正确地大写并不是您应该准备处理的唯一潜在错误。你知道吗

相关问题 更多 >

    热门问题