如何在 if(if)期间之后赋值



为什么我不能分配 ES 的值?我的值是 .5、13 和 2,尽管还没有来自 2这是错误

在 打印("津贴 ="),ES 名称错误:未定义名称"ES"

import math
D = input("Nominal Size: ")
TPI = input("TPI: ")
TOL = input("Tolerance Class 1, 2, 3: ")
P = float(1.0/TPI)
H = float((math.sqrt(3)/2)*P)
D2 = float(D-2*.375*H)
D1 = float(D-2*.625*H)

if ((0.05*(pow(P,2)**(1/3))+(0.03*(P/D)))-0.002) < (0.25*P)-(0.4*pow(P,2)):
    ES = float((0.25*P)-(0.4*pow(P,2)))
print ""
print ("Pitch ="), P
print ("Fundamental Triangle Height ="), H
print ("Allowance ="), ES
print ("Basic Pitch Diameter ="), D2
print ("Basic Minor Diameter ="), D1
只有在不可

穿透的条件下设置ES

((0.05*(pow(P,2)**(1/3))+(0.03*(P/D)))-0.002) < (0.25*P)-(0.4*pow(P,2))

是真的。显然不是。

如果存在合适的ES默认值,您可以事先设置该值,或者在else子句中设置该值:

ES = 0    # whatever default is valid
if ((0.05*(pow(P,2)**(1/3))+(0.03*(P/D)))-0.002) < (0.25*P)-(0.4*pow(P,2)):
    ES = float((0.25*P)-(0.4*pow(P,2)))

if ((0.05*(pow(P,2)**(1/3))+(0.03*(P/D)))-0.002) < (0.25*P)-(0.4*pow(P,2)):
    ES = float((0.25*P)-(0.4*pow(P,2)))
else:
    ES = 0    # whatever default is valid

如果没有合适的默认值,则可以初始化/设置ES None,然后在打印时显式检查:

print ("Allowance ="), ES if ES is not None else '?'

(或者可能'None'实际上是印刷品的合理事物)

只需在要使用的同一作用域上分配一个默认值即可。如果条件的计算结果未为 true,则将打印 0。

ES = 0
if ((0.05*(pow(P,2)**(1/3))+(0.03*(P/D)))-0.002) < (0.25*P)-(0.4*pow(P,2)):
    ES = float((0.25*P)-(0.4*pow(P,2)))
print ES
好吧,

我想通了。谢谢大家。我正在做,提高到幂而不是提高幂,这把我搞砸了,我从 1/3 更改为 .333333 我添加了 ES,更改为 TD1,在 if 语句上方。我添加了 P1 做一个指数,然后让它达到 if 作为提高它的一半,然后再次提高它。对于那些没有抓住的人,我是python的新手。并且,这是计算螺纹直径主要次要和螺距主要次要

import math
D = input("Nominal Size: ")                  ##Nominal thread size
TPI = input("TPI: ")                         ##Thread per inch
TOL = input("Tolerance Class 1, 2, 3: ")     ##Class 1A 2A 3A 1B 2B 3B
P = float(1.0/TPI)                           ##Thread pitch
H = float((math.sqrt(3)/2)*P)                ##Height of fundamental triangle
D2 = float(D-2*.375*H)                       ##Thread basic pitch diameter
D1 = float(D-2*.625*H)                       ##Thread basic minor diameter
P1 = pow(P,2)                                ##Lots of exponent stuff
SDT = .54127*P                               ##Single depth of threads
DDT = SDT*2                                  ##Double depth of threads
MiMDI = D-DDT                                ##Minimum Minor Diameter Internal
MiPDI = (D-(0.32475953*P)*2)                 ##Minimum Pitch Diameter Internal
ES = 0                                       ##Allowance
Td = 0                                       ##Major diameter tolerance of external threads
Td2 = 0                                      ##Pitch diameter tolerance of external threads
TD1 = 0                                      ##Minor diameter tolerance of internal threads
TD2 = 0                                      ##Pitch diameter tolerance of internal threads
if ((0.05*pow(P1,.33333333333)+(0.03*(P/D)))-0.002) < (0.25*P)-(0.4*pow(P,2)):
    TD1 = float((0.25*P)-(0.4*pow(P,2)))
MaMDI = MiMDI+TD1                                ##Maximum Minor Diameter Internal
print ""
print ("Pitch ="), P
print ("Fundamental Triangle Height ="), H
print ("Allowance ="), ES
print ("Basic Pitch Diameter ="), D2
print ("Basic Minor Diameter ="), D1
print ""
print ("INTERNAL THREADS")
print ("MINIMUM Minor Diameter ="), MiMDI
print ("MAXIMUM Minor Diameter ="), MaMDI
print ("MINIMUM Pitch Diameter ="), MiPDI
##print ("MINIMUM Pitch Diameter ="), MaPDI

最新更新