如何根据训练类型计算值?

时间:2017-10-22 23:07:45

标签: python conditional

我有几种具有关联值的编号训练类型。

可能的培训类型:

  1. 小:0.5
    大:0.7

  2. 小:0.7
    大:0.8

  3. 等...

  4. 如果选择训练类型编号1,如何确定用于计算的相关值对?例如,如果培训类型为1:

    small = (220 - 60)*0.5
    big = (220 - 60)*0.7
    

    我想知道如何编写代码,以便后续计算中使用的值根据所选的训练类型而有所不同。

    到目前为止我所拥有的:

    training = str(input("Choose training type (1, 2, 3): "))
    s1 = 0.5
    s2 = 0.7
    s3 = 0.8
    b1 = 0.7
    b2 = 0.8
    b3 = 0.88
    spulse = "Small pulse: "
    bpulse = "Big pulse: "
    if training == 1:
        small = (220 - 60) * s1 
        big = (220 - 60) * b1
    elif training == 2:
        small = (220 - 60) * s2
        big = (220 - 60) * b2
    elif training == 3:
        small = (220 - 60) * s3
        big = (220 - 60) * b3
    
    print(spulse + str(small) + bpulse + str(big))
    

3 个答案:

答案 0 :(得分:0)

如果您使用内置输入功能,您可以执行以下操作:

question=input('Choose type 1 or type 2: ')
if question=='1':
        small=(220-60)*0.5
        big=(220-60)*0.7
        type1_total=small+big
print(type1_total)

结果:

192.0

答案 1 :(得分:0)

如果您将input转换为int而不是str并且在决策范围之外初始化smallbig,则您的代码将有效这样的陈述:

training = int(input("Choose training type (1, 2, 3): "))
s1 = 0.5
s2 = 0.7
s3 = 0.8
b1 = 0.7
b2 = 0.8
b3 = 0.88
spulse = "Small pulse: "
bpulse = "Big pulse: "
small = 0
big   = 0
if training == 1:
    small = (220 - 60) * s1 
    big = (220 -60) * b1
elif training == 2:
    small = (220 - 60) * s2
    big = (220 -60) * b2
elif training == 3:
    small = (220 - 60) * s3
    big = (220 -60) * b3

但是,一种替代方法可能如下:

value = 220 - 60
type_ = 0
types = {1 : [0.5, 0.7], 
         2 : [0.7, 0.8],
         3 : [0.8, 0.88]}

while type_ not in types:

    type_ = int(input("Pick a type: "))

    if type_ not in types:

        print("Invalid type.")

    else:

        big   = value * types[type_][0]
        small = value * types[type_][1]

print("Big   = " + str(big))
print("Small = " + str(small))

这样,如果用户在提示符处输入1作为type_的值,则输出为:

Big   = 80.0
Small = 112.0

但是,如果用户在提示符处输入2作为type_的值,则输出为:

Big   = 112.0
Small = 128.0

如果用户在提示符处输入3作为type_的值,则输出为:

Big   = 128.0
Small = 140.8

对于输入的任何其他值,输出为print("Invalid type.")

答案 2 :(得分:0)

在Python 3中,input()函数返回一个字符串(与Python 2不同),但是所有if training == ...语句都将它返回的值与一个整数进行比较,因此它们总是会失败。要修改第一行,如下所示:

#training = str(input("Choose training type (1, 2, 3): "))  # NOT THIS.
training = int(input("Choose training type (1, 2, 3): "))  # Convert to integer.
s1 = 0.5
s2 = 0.7
s3 = 0.8
b1 = 0.7
b2 = 0.8
b3 = 0.88
spulse = "Small pulse: "
bpulse = " Big pulse: "
if training == 1:
    small = (220 - 60) * s1
    big = (220 - 60) * b1
elif training == 2:
    small = (220 - 60) * s2
    big = (220 - 60) * b2
elif training == 3:
    small = (220 - 60) * s3
    big = (220 - 60) * b3

print(spulse + str(small) + bpulse + str(big))

产生的结果:

Small pulse: 80.0 Big pulse: 112.0

<强>更新

最好使用Python字典来执行此操作。这样做 - 被称为“数据驱动” - 通过添加更多培训类型和/或具有与每个培训类型相关联的更多值,也将使调试和扩展更容易。我认为这也使代码更清晰,更具可读性。

这显示了我的意思:

# Dictionary associating each training type to associated values.
training_types = {
    "1": {"s": 0.5, "b": 0.7},
    "2": {"s": 0.7, "b": 0.8},
    "3": {"s": 0.8, "b": 0.88}
}

choice = None
while choice not in training_types:
    choice = input("Choose training type (1, 2, or 3): ")

training_type = training_types[choice]
difference = 220 - 60
small = difference * training_type["s"]
big = difference * training_type["b"]

print("Small pulse: {}  Big pulse: {}".format(small, big))