简体   繁体   中英

How to find total amount from given ratios using python

Here we have one condition, here we have given three ratios for example 2: 3: 5(ratios can be change) and we have value of last ratio for example 5 = 10,000 so write a python code to find total amount.

Using some math

# INDEX -3 -2 -1    
ratio = [2, 3, 5]
# INDEX  0  1  2

# let ratio to 2x, 3x, and 5x.

last_value = 10000 # 5x value
x = last_value/ratio[-1] ## finding the value of x

total = 0
for a in ratio:
    total+=a*x

print(total) # → 20000.0

OR

# INDEX -3 -2 -1    
ratio = [2, 3, 5]
# INDEX  0  1  2

# let ratio to 2x, 3x, and 5x.

last_value = 10000 # 5x value
total = sum(a*(last_value/ratio[-1]) for a in ratio)
print(total) # → 20000.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM