使用 while 背后的逻辑

问题描述 投票:0回答:4

我最近开始接触编程(我正在使用Python)。 我想知道是否有人可以向我解释为什么“try”在循环中重复,而写“print(“我的房子”)”的循环不重复。

我附上一张图片用于解释目的:

def a():
    try:      
        if 3> 0:
           print("This space is occupied, try another!")
           return False
        print("   0  1  2")                  
    except IndexError:
        print("Did you attempt to play a row or column outside the range of 0,1 or 2? (IndexError)")
        return False
    
play = True
while play:
    print ("my house")
    game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]


    game_won = False
    while not game_won:
        print ("a")
        b=a()
python while-loop
4个回答
3
投票

两个循环,并且内部循环永远不会终止(因为你永远不会更新

game_won
,所以你永远不会继续到外部循环的第二次迭代。


0
投票

首先, while 播放循环将开始,并且会一直持续到 while not game_won 循环,并且由于 not game_won 为 True,它将启动该循环并且不会退出,直到 not game_won 为 False


0
投票

你有两个无限循环。以谓词终止的内部循环;

not game_won
内部循环中的逻辑无限期地继续,而不返回到外部上下文,因为循环中的任何点都不会设置
game_won=True

def a():
    try:      
        if 3> 0:
           print("This space is occupied, try another!")
           return False
        print("   0  1  2")                  
    except IndexError:
        print("Did you attempt to play a row or column outside the range of 0,1 or 2? (IndexError)")
        return False
    
play = True
while play:
    print ("my house")
    game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]


    game_won = False
    while not game_won:
        print ("a")
        b=a()
        if 1==1:
            break  # This will cause exit.

0
投票
def a():
    try:      
        if 3> 0:
           print("This space is occupied, try another!")
           return false
        print("   0  1  2")                  
    except IndexError: # You never reach this point because 3 > 0: is the same as just putting if 1==1 it always reads as a true statement
        print("Did you attempt to play a row or column outside the range of 0,1 or 2? (IndexError)")
        return False
    
play = True
while play: # 1. while True gets executed on the first iteration
    print ("my house")
    game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]


    game_won = False
    while not game_won: # 2. you get stuck here and is the same as While True unless you specify when the game is won
        b=a()

基本上,您陷入了第二个循环,仅调用函数 a() 多次输入代码的 try: 部分,您可以通过在函数 a 中添加循环计数器来看到这一点,以帮助您识别发生的情况

i=0
def a():
    global i
    try:      
        if 3> 0:
            print("This space is occupied, try another!")
            i=i+1
            print(f"you have ran a() {i} times")
            return False
        print("   0  1  2")
    except IndexError: # You never reach this point because 3 > 0: is the same as just putting if 1==1 it always reads as a true statement
        print("Did you attempt to play a row or column outside the range of 0,1 or 2? (IndexError)")
        return False

也许这个逻辑会帮助你更好地理解代码是如何执行的

def a():
    x=4*4 #2 just some math that results in 16
    return x #3 you return x so it can be accessed outside of the function if you did not add this when print(b) is called it will result in a None type
while True:
    b=a() # 1. ) this calls the function a() and stores the result in the variable b
    print(int(b))
© www.soinside.com 2019 - 2024. All rights reserved.