Python 为什么while循环在第一次运行后不循环?While循环无法读取变量的新值

Python 为什么while循环在第一次运行后不循环?While循环无法读取变量的新值,python,python-3.x,variables,while-loop,null,Python,Python 3.x,Variables,While Loop,Null,我是Python新手,想知道为什么会发生这种情况。 代码如下: import turtle What_Calculate = turtle.numinput("Area of Different Shapes Calculator", "Type 1 to calculate the area of a square, Type 2 to calulate the area of a rectangle, Type 3 to calculate the area if a triangle,

我是Python新手,想知道为什么会发生这种情况。 代码如下:

import turtle


What_Calculate = turtle.numinput("Area of Different Shapes Calculator", "Type 1 to calculate the area of a square, Type 2 to calulate the area of a rectangle, Type 3 to calculate the area if a triangle, Type 4 to calulate the area of a circle, or click [q] to quit:")

while What_Calculate !="q":

    if What_Calculate == 1:
        Length_Square = eval(input("What is the length of the square?:"))
        Area_Squ = Length_Square * Length_Square
        print("The area of the square is ", Area_Squ, ".")
        What_Calculate = input("Click another number or click [q] to exit:")

    elif What_Calculate == 2:
        Length_1 = eval(input("What is the first length?:"))
        Length_2 = eval(input("What is the second length?:"))
        Area_Rec = Length_1 * Length_2
        print("The area of the rectangle is ", Area_Rec, ".")
        What_Calculate = input("Click another number or click [q] to exit:")

    elif What_Calculate == 3:
        Length = eval(input("What is the length?:"))
        Height = eval(input("What is the height?:"))
        Area = Length * Height / 2
        print("The area of the triangle is", Area)
        What_Calculate = input("Click another number or click [q] to exit:")

    elif What_Calculate == 4:
        Diameter = eval(input("What's the diameter of the circle?:"))
        Radius = Diameter / 2
        Pie = 3.14
        Area = Pie * Radius * Radius
        print("The area of the circle is ", Area, ".")
        What_Calculate = input("Click another number or click [q] to exit:")
问题是: 在用户输入他们想要使用的计算器之后,代码一直工作到给出答案为止。
在What_Calculate=Input点击另一个数字或点击[q]退出:,输入q退出效果很好,但1-4根本不起作用。如果我没有错,在这种情况下,while不应该循环,直到What_Calculate为null吗?

返回一个字符串。。。在检查int时,输入函数返回一个字符串,由于循环中的所有比较都是数字,因此它将在第一次通过后永远循环。您不应该同时使用What_Calculate作为int和字符串-我的建议是尽可能少地更改代码,将比较从What_Calculate==1:更改为What_Calculate==1:等等,然后更改turtle.numinput调用,要求输入字符串而不是数字。

问题是,What_Calculate=input单击另一个数字或单击[q]退出:返回字符串,该字符串在if/elif/elif/elif中总是失败。因为它是字符串而不是数字,所以1!=1.我所能看到的最简单的方法是添加

try:
    What_Calculate = int(What_Calculate)
except ValueError:
    #Some code to handle what happens if What_Calculate cannot be turned into an int.

到while循环的开始。将其包含在try/except循环中,可以检测用户输入的内容是否不是整数,也不是字母“q”。

@martineau true。修好了,谢谢!我真的很感激。非常感谢你的帮助!