将增加的数字转换为

时间:2018-04-21 19:49:40

标签: python list

我使用电阻带来测量我的泳池控制系统中的水位。我需要将此值转换为一个数字,表示池的运行百分比值以运行我的系统。

我目前的方法有效,但我想找出一种更好的方法。我确实看过这样做的其他方法,但我的问题是我的电阻值与我正在寻找的转换数字相反,我无法使数学运算。

例如,电阻值785 = 100,786 = 99,787 = 98,788 = 97等等。

以下是我目前的情况,但我确信有更好的方法:

# Convert Pool Resistance reading to Percentage for web interface
def get_pool_level_percentage(level):
global pool_level_percentage
if level <= 785:
    pool_level_percentage = 100
elif level == 786:
    pool_level_percentage = 99
elif level == 787:
    pool_level_percentage = 97
elif level == 788:
    pool_level_percentage = 96
elif level == 789:
    pool_level_percentage = 95
elif level == 700:
    pool_level_percentage = 94
elif level == 791:
    pool_level_percentage = 93
elif level == 792:
    pool_level_percentage = 92
elif level == 793:
    pool_level_percentage = 91
elif level == 794:
    pool_level_percentage = 90
elif level == 795:
    pool_level_percentage = 89
elif level == 796:
    pool_level_percentage = 88
elif level == 797:
    pool_level_percentage = 87
elif level == 798:
    pool_level_percentage = 86
elif level == 799:
    pool_level_percentage = 85
elif level == 800:
    pool_level_percentage = 84
elif level == 801:
    pool_level_percentage = 83
elif level == 802:
    pool_level_percentage = 82
elif level == 803:
    pool_level_percentage = 81
elif level == 804:
    pool_level_percentage = 80
elif level == 805:
    pool_level_percentage = 79
elif level == 806:
    pool_level_percentage = 78
elif level == 807:
    pool_level_percentage = 77
elif level == 808:
    pool_level_percentage = 76
elif level == 809:
    pool_level_percentage = 75
elif level == 810:
    pool_level_percentage = 74
elif level == 811:
    pool_level_percentage = 73
elif level == 812:
    pool_level_percentage = 72
elif level == 813:
    pool_level_percentage = 71
elif level == 814:
    pool_level_percentage = 70
else:
    pool_level_percentage = 69
return pool_level_percentage

1 个答案:

答案 0 :(得分:4)

在您的说明中,级别787映射到98%,而在代码级别787映射到97%。假设这是一个拼写错误,787级应该映射到98%,788到97%等,下面的代码应该可以解决问题。我还假设pool_level_perentage可以取小于69的值(某些内容未反映在您的代码中),因此我不会包含任何其他条件。

def get_pool_level_percentage(level):

  if (level > 785):
    return (100 - (level - 785))

  return 100