如何在\ after之后分配一个值

时间:2016-03-18 01:24:04

标签: python python-2.7 if-statement assign

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

  

在       print(" Allowance ="),ES NameError:name' 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

3 个答案:

答案 0 :(得分:3)

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'实际上是合理的印刷品)

答案 1 :(得分:0)

只需在要使用它的同一范围内指定默认值即可。如果条件未评估为真,则将打印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

答案 2 :(得分:0)

好吧,我弄明白了。多谢你们。我正在做,提高功率而不是提高功率,这让我搞砸了,我从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