一段时间内并不总是需要凹痕吗?

发布于 2025-02-06 05:38:23 字数 876 浏览 3 评论 0原文

为什么此代码工作?我以为我应该将所有内容都置于循环中,但是如果这样做,循环就会破裂。相反,如果我不缩进,那就起作用。对我来说似乎是倒退的。谁能解释这里发生了什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒的傷心 2025-02-13 05:38:23

我会尝试解释,但是我的英语不好,对不起!

  • 在这种情况下,您需要知道的第一件事是在情况下满足条件时的结束
  • !结束循环您将转到IF条件,但您不需要其他条件,因为答案将是“ X”或“ O”。
  • 当您需要使用2个或更多条件时,您应该使用
if condition 1
elif condition 2
else

- 第二件事是要知道的是,当您返回功能时,您就会停止该过程。
然后,当您返回并验证功能中的答案时,will会自动停止。
例如。

def numbers(num):
 print(1)
 if num == 1:
   return "end"
 print(2)
 if num == 2:
   return "end"
 print(3)
 if num == 3:
   return "end"
  • 第二个功能也有效,但暂时是没有用的,如果您在whitout时编写函数,则相同。这是因为,无论答案如何,您总是会返回一些东西。

I'll try to explain, but my english is not good, sorry!

  • the first thing you need to know that the while ends when the condition is met in this case != (answer == "X" or answer == "O"),
  • In the first case, it works, and when the at the end loop you will go to the if condition, but you don't need the else, because the answer will be "X" or "O".
  • When you need to use 2 or more conditions, you should use
if condition 1
elif condition 2
else

-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.

def numbers(num):
 print(1)
 if num == 1:
   return "end"
 print(2)
 if num == 2:
   return "end"
 print(3)
 if num == 3:
   return "end"
  • The second function, also works, but the while is useless, it's the same if you write the function whitout the while. It's because, you always return something, regardless of the answer.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文