python - 记录循环生成的数字

标签 python loops variables

我正在创建一个简单的程序来计算应添加每种成分的量,以创建每种成分所需最终浓度的缓冲液。我希望用户能够根据需要向缓冲区添加尽可能多的组件。对于每个组件,我都有一个循环,它会告诉用户每个组件要添加多少,但我似乎找不到一种方法将每个值保存为变量并稍后将其减去总缓冲量。简而言之,我想在循环运行后记录每个组件的值,但我不知道如何实现这一点。这是我到目前为止所拥有的

Buffer_Volume = input('What does the final volume of the buffer need to be, in mL? ')
Final_Volume = float(Buffer_Volume)

Component_Number = int(input('How many components does this buffer have? '))

x = 0
for  x in range(0, Component_Number):
    Stock = input('What is the molarity of the stock component? ')
    Stock_Molarity = float(Stock)

    Final = input('What does the final molarity of the component need to be? ')
    Final_Molarity = float(Final)

    #Volume = input('What does the final volume of the buffer need to be, in mL? ')
    #Final_Volume = float(Volume)

    Stock_Volume = (Final_Molarity * Final_Volume) / Stock_Molarity
    print('Add ' + str(round(Stock_Volume, 6)) + 'mL of stock component to the solution')

print('Add ' + str((Final_Volume - Stock_Volume)) + 'mL of water to the solution')

最佳答案

如果您需要的只是从最终体积中减去总库存量,那么您需要的只是在循环中递增的附加变量:

Buffer_Volume = input('What does the final volume of the buffer need to be, in mL? ')
Final_Volume = float(Buffer_Volume)

Component_Number = int(input('How many components does this buffer have? '))

x = 0
Stock_total = 0.
for  x in range(0, Component_Number):
    Stock = input('What is the molarity of the stock component? ')
    Stock_Molarity = float(Stock)

    Final = input('What does the final molarity of the component need to be? ')
    Final_Molarity = float(Final)

    #Volume = input('What does the final volume of the buffer need to be, in mL? ')
    #Final_Volume = float(Volume)

    Stock_Volume = (Final_Molarity * Final_Volume) / Stock_Molarity
    print('Add ' + str(round(Stock_Volume, 6)) + 'mL of stock component to the solution')

    Stock_total += Stock_Volume
print('Add ' + str((Final_Volume - Stock_total)) + 'mL of water to the solution')

如果您需要对计算出的这些值执行更多操作,只需将它们存储在列表中即可:

Buffer_Volume = input('What does the final volume of the buffer need to be, in mL? ')
Final_Volume = float(Buffer_Volume)

Component_Number = int(input('How many components does this buffer have? '))

x = 0
Stocks = []
for  x in range(0, Component_Number):
    Stock = input('What is the molarity of the stock component? ')
    Stock_Molarity = float(Stock)

    Final = input('What does the final molarity of the component need to be? ')
    Final_Molarity = float(Final)

    #Volume = input('What does the final volume of the buffer need to be, in mL? ')
    #Final_Volume = float(Volume)

    Stock_Volume = (Final_Molarity * Final_Volume) / Stock_Molarity
    print('Add ' + str(round(Stock_Volume, 6)) + 'mL of stock component to the solution')
    Stocks.append(Stock_Volume)

print('Add ' + str((Final_Volume - sum(Stocks))) + 'mL of water to the solution')

关于python - 记录循环生成的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49160579/

相关文章:

matlab - 如何向量化嵌套循环

C++计算一行中有多少个单词

arrays - 有没有办法在用户输入特定数字之前创建数组?

html - 如何使用 Bulma 的默认 $ 变量

python - 图像不响应不同的按键 Pygame Python

function - 连接路径和文件

python - tastypie - 禁用嵌套对象创建

python - 如何将 numpy 数组分解成更小的 block /批处理,然后遍历它们

python - 网格置换算法 - 固定行顺序

python - 创建字典,其中键是变量名