在python中无法突破while循环

时间:2017-02-18 17:51:05

标签: python python-3.x while-loop breakpoints

无法在python中突破while循环: 我尝试将代码合并在一起,没有main和getheight函数,它仍然给我一个无限循环。

def main():
    x = 0
    while x not in range (1,23):
        getheight()
        if x in range (1,23):
            break
    for i in range (x):
        for j in range (x - j):
            print (" ", end="")
        for j in range (x):
            print ("#", end="")
        print ("  ", end="")
        for j in range (x):
            print ("#", end="")
        "\n"

def getheight():
    x = input("Give me a positive integer no more than 23 \n")
    return x

if __name__ == "__main__":
    main()

2 个答案:

答案 0 :(得分:1)

main()中的{p> xgetheight()x变量独立的局部变量。您将从后者返回新值,但会返回返回的值。在函数调用结果中设置main中的while x not in range (1,23): x = getheight() if x in range (1,23): break

getheight()

您还需要修复def getheight(): x = input("Give me a positive integer no more than 23 \n") return int(x) 函数以返回整数,而不是字符串:

mydomain.com:
Incoming email to pete@mydomain.com -> htaccess -> redirecting to myemail@gmail.com

答案 1 :(得分:0)

返回并不意味着它会自动更新main中的任何变量名称与返回变量匹配。如果不存储此返回值,则返回值将消失。因此你必须这样做

x=getheight()

更新变量x