一段时间内并不总是需要凹痕吗?
为什么此代码工作?我以为我应该将所有内容都置于循环中,但是如果这样做,循环就会破裂。相反,如果我不缩进,那就起作用。对我来说似乎是倒退的。谁能解释这里发生了什么?
PS显然我是一个菜鸟。
#This works
def choose_team():
answer = ""
while not (answer == "X" or answer == "O"):
answer = input("Choose X eller O: ")
if answer == "X":
return("You are player 1")
if answer == "O":
return("You are player 2")
else:
return("Pick X or O ffs, moron")
#This DOES NOT WORK!
def choose_team():
answer = ""
while not (answer == "X" or answer == "O"):
answer = input("Choose X eller O: ")
if answer == "X":
return("You are player 1")
if answer == "O":
return("You are player 2")
else:
return("Pick X or O ffs, moron")
Why does this code work? I thought I should indent everything in a loop, but if I do, the loop breaks off. Instead, if I DON'T indent, it works. Seem backwards to me. Can anyone explain what's going on here?
P.S. Obviously I'm a total noob.
#This works
def choose_team():
answer = ""
while not (answer == "X" or answer == "O"):
answer = input("Choose X eller O: ")
if answer == "X":
return("You are player 1")
if answer == "O":
return("You are player 2")
else:
return("Pick X or O ffs, moron")
#This DOES NOT WORK!
def choose_team():
answer = ""
while not (answer == "X" or answer == "O"):
answer = input("Choose X eller O: ")
if answer == "X":
return("You are player 1")
if answer == "O":
return("You are player 2")
else:
return("Pick X or O ffs, moron")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
我会尝试解释,但是我的英语不好,对不起!
- 第二件事是要知道的是,当您返回功能时,您就会停止该过程。
然后,当您返回并验证功能中的答案时,will会自动停止。
例如。
I'll try to explain, but my english is not good, sorry!
-The second thing to know, is when you return a function, you stop the process.
Then when you return and validate the answer in your function, the while is automatically stopped.
eg.