"Name can be undefined" python 中的问题

"Name can be undefined" problem in python

我正在尝试改进我的代码,所以这是关于 'problem notification' 告诉我变量可以未定义的。 这是我的代码:

print('\nWindow types')
print('1:1200 x 1300 mm')
print('2:800 x 1000 mm')
print('3:2100 x 1500 mm')
Window_Dimension = float(input('Select window type: '))
if Window_Dimension == 1:
    A = 1200
    B = 1300
elif Window_Dimension == 2:
    A = 800
    B = 1000
elif Window_Dimension == 3:
    A = 2100
    B = 1500
else:
    print('Invalid input!')

Lip_height = float(input('\nEnter lip eight (mm): '))
SWind_velocity = 80  # m/s Static wind velocity
print('Assuming a peak wind velocity of: 80 m/s')
Wind_pressure = (SWind_velocity ** 2) / 1600  # kN/m^2
print('Wind pressure (kN/m^2):', Wind_pressure)
a = A - (2 * Lip_height)
b = B - (2 * Lip_height)
Lip_area = (A * B) - (a * b)  # m^2
Lip_pressure = (Wind_pressure * (A * B) / 1000000) / (Lip_area / 1000000)  # kN/m^2
print('Uniform pressure on the lip (kN/m^2): ', round(Lip_pressure, 3))

它工作得很好,但我不断收到关于这两行的问题通知:

a = A - (2 * Lip_height)
b = B - (2 * Lip_height)

告诉我 A 和 B 可以是未定义的。我不知道是否有办法解决这个问题,或者即使我应该解决它,但我是 python 的新手,我正在努力避免未来的“编码坏习惯”。谢谢

当出现问题时,不要简单地打印一条错误消息并继续。抛出异常。而不是

print('Invalid input!')

考虑

raise Exception('Invalid input!')

据我所知,它运行良好。我没有收到任何错误。也许您需要在一开始就定义变量。 在开头,将此添加到:

a = 0
b = 0

这在一开始就设置了变量,没有计算。后来,它只会重写变量,而不是创建变量。

让我知道这是否有效!