如何根据Python中的输入答案计算条件?

时间:2017-11-14 12:22:18

标签: python calculator

需要一些帮助来解决这个问题。我没有在搜索中找到任何内容,但这可能是因为我不完全确定要问什么或定义问题。

我会在这里尝试这样做。

所以我想为潜水做一个SAC计算器。在lamens术语中,基本上是计算空气消耗的计算器。

度量的公式是VT x VC / T / P = SAC 更多信息https://www.divein.com/guide/know-your-air-consumption/

我在计算空气消耗时遇到了一些问题,因为我似乎需要以某种方式将用户的输入转换为值。

  • 用户回答问题,您的潜水深度以米(或英尺)为单位?嗯,这是非常直接的,因为用户会回答,即30米/ 100英尺。

然而,为了使计算器正常工作,我需要使用ATM模型进行计算。随着水深的增加,大气压力也会增加,如图所示:

Pressure Example

因此,如果用户输入30米的值,我需要知道此答案的ATM值。

我尝试制作一个if语句来解决这个问题,但我不相信这可能是更好的方法。

这是迄今为止的代码

    #SAC Rate Calculator
#This program will calculate your air consumption rate for scuba diving
#Made by Tom Knudsen - post@tknudsen.com
#License is open source

import os
import time

os.system('clear')
print('***********************************')
print('***********************************')
print('**********SAC Calculator***********')
print('****************by*****************')
print('************Tom Knudsen************')
print('***********************************')
print('***********************************')
print('')
print('')
print('Welcome to the SAC calculator')
time.sleep(1)

# This code is to first determain the air consumption in bar)
print('First we need to calculate your air consumption in bar')
tankVolum = input('How large is your tank in litres? :')
startBar = input('Please enter your start pressure in BAR: ') #starting pressure with full cooled tank
endBar = input('Please enter your end pressure in BAR: ') #ending pressure with cooled tank
totalBar = startBar - endBar
time.sleep(2)
print('Thank you, your total pressure used is: ') #feedback to the user
print(totalBar)
time.sleep(3)
os.system('clear')
# Input to calculate air consumption in litres
print("Let's now see how much Surface Air Concumption in litre you used!")
time.sleep(2)
print('First we need to know a little bit about your dive :')
time.sleep(2)
depth = input('How deep was your dive in meter? :')
time.sleep(2)
diveTime = input('How long was your dive in minutes? :')
time.sleep(2)
print('Let me calculate, please wait...')


#this is my test, I do not know how to get "depthTotal" as this calue equals ATM pressure and P in the equation formula above.
if depth <= 10:
    a = 2
elif depth >10 and <= 20:
    b = 3
elif depath >20 and <= 30:
    c = 4
    else:
        print('You need to enter a depth between 0 and 40 meter')



sacResults = tankVolum x totalBar / diveTime / depthTotal

 # Example =  12 Liter x 150 bar used / 46 minutes dive time / 30 meter depth
 # sacResults = 12 * 150 / 46 / 3

2 个答案:

答案 0 :(得分:1)

解决问题的最简单方法是执行以下操作:

#this is my test, I do not know how to get "depthTotal" as this calue equals ATM pressure and P in the equation formula above.

depth_total = None
if depth <= 10:
    depth_total  = 2
elif depth >10 and <= 20:
    depth_total  = 3
elif depath >20 and <= 30:
    depth_total  = 4
    else:
        print('You need to enter a depth between 0 and 40 meter')

sacResults = tankVolum x totalBar / diveTime / depthTotal

你可以用更简单的方式做到这一点,因为看起来ATM与深度成正比(每33英尺压力为1巴),你可以创建一个简单的功能,将深度转换成ATM:

def depth_to_atm(depth_in_meters):
    (depth_in_meters*3.2808)/ 33 # apparently 1 bar equals 33 feet of water 
                                 # (3.2808 is how many feet there are in a meter)

在公式中使用该功能。

答案 1 :(得分:0)

计算ATA的公式很简单。

您不需要使用条件来解决这个问题,使用此公式可以更准确地表示ATA。

ATA = depth / 10 + 1

使用此功能,当您在整数深度之间时,您将获得正确的ATA。