Python 当我们在while循环中定义变量时,如何定义它?

Python 当我们在while循环中定义变量时,如何定义它?,python,python-3.x,coding-style,Python,Python 3.x,Coding Style,现在我想知道,在运行代码时,它一直说变量未声明,它说在公式中,我使用的变量没有定义,但我已经在while循环中定义了它们,当用户选择某个计算时,我只想带来某些输入。请帮忙! 我做错了什么?请帮忙 谢谢只需执行if子句中的特定计算即可。代码的问题是,如果用户没有选择相应的选项,某些变量就无法定义。因此,计算将失败 e、 g 这将确保所有必要的变量都定义在需要的位置。只需执行if子句中的特定计算即可。代码的问题是,如果用户没有选择相应的选项,某些变量就无法定义。因此,计算将失败 e、 g 这将确保所

现在我想知道,在运行代码时,它一直说变量未声明,它说在公式中,我使用的变量没有定义,但我已经在while循环中定义了它们,当用户选择某个计算时,我只想带来某些输入。请帮忙! 我做错了什么?请帮忙
谢谢

只需执行if子句中的特定计算即可。代码的问题是,如果用户没有选择相应的选项,某些变量就无法定义。因此,计算将失败

e、 g


这将确保所有必要的变量都定义在需要的位置。

只需执行if子句中的特定计算即可。代码的问题是,如果用户没有选择相应的选项,某些变量就无法定义。因此,计算将失败

e、 g


这将确保所有必要的变量都在需要的位置定义。

您必须使用
Answer=input(“Choice:”)
内部执行一个大while循环。不是x while循环不起作用。为什么要使用
while
循环,就像
if
语句一样?你必须在
Answer=input(“Choice:”)
内部执行一个大while循环。不是x while循环不起作用。为什么要使用
while
循环,就像
if
语句一样?
G  = float(6.67 * (10**-11))
print("Choose from the following '\n a) Gravitational Force '\n b) Density '\n c) Kinetic Energy '\n d) Potential Energy '\n e) Work Done '\n f) Weight '\n g) Force ")

Answer = input("Choice: ")
 while Answer == 'a':
    M1 = float(input("Enter mass of first body: "))
    M2 = float(input("Enter mass of second body: "))
    r = float(input("Enter distance between them: "))
    break
 while Answer == 'b':
    vol = float(input("Enter volume of the object: "))
    m = float(input("Enter mass of the body: "))
    break
while Answer == 'c':
    m1 = float(input("Enter mass of the body: "))
    v = float(input("Enter velocity of the body: "))
    break
while Answer == 'd':
    g = float(input("Enter value of acceleration due to gravity: "))
    m2 = float(input("Enter mass of the body: "))
    h = float(input("Enter height of the object: "))
    break
while Answer == 'e':
    f = float(input("Enter vaue of force applied: "))
    s = float(input("Enter value of displacement: "))
    break
while Answer =='f':
    g1 = float(input("Enter value of acceleration due to gravity: "))
    m3 = float(input("Enter mass of the body: "))
    break
while Answer == 'g':
    m4 = float(input("Enter mass of the body: "))
    acc = float(input("Enter value of acceleration: "))
    break

D = m/vol
W = m3*g1
F = m4*acc
GF = (G * M1*M2)/r**2
WD = f*s
KE = (1/2 * m1 * v**2)
PE = m2*g*h

if Answer == 'a':
    print(GF)
elif Answer == 'b':
    print(D)
elif Answer == 'c':
    print(KE)
elif Answer == 'd':
    print(PE)
elif Answer == 'e':
    print(WD)
elif Answer == 'f':
    print(W)
elif Answer == 'g':
    print(F)
if Answer == 'a':
    GF = (G * M1*M2)/r**2
    print(GF)