0

is there a way to simplify the code for commission below?

money2 = int(input("how much to withdraw? "))
commission = max(min(round(money2*(10/100)),100),50)

print("the commission is {0}".format(commission))

the former code was used with if. python(or pylint maybe?) adviced me to use min and max. I think it was because this code is included in a function with if itself (the whole code is in the bottom)

money2 = int(input("how much to withdraw? "))
commission = round(money2*(10/100))

if commission > 100:
    commission = 100

elif commission < 50:
    commission = 50

print("the commission is {0}".format(commission))

the first code is short but not quite quick to read, and the second(or the former) is using too much lines.

is there a way to simplify it? I just want the value of commission to always be at least 50, and 100 at max.

ps. below is the whole code i'm practicing on. My intention was to create a loop of deposit() and withdraw() untill the balance runs out, and if i input more than what i can withdraw, it will ask me again how much to withdraw (so a loop in a loop). My original question is in line 10. Perhaps you could give me other advice for simplifying other parts of this code too. Thanks

def deposit():
  money = int(input("how much to deposit? "))
  print("{0} has been depostied. total {1}"\
        .format(money, balance + money))
  return balance + money;

def withdraw():
    while True:
       money2 = int(input("how much to withdraw? "))
       commission = max(min(round(money2*(10/100)), 100),50)

       if money2 + commission > balance:
          print("not enough balance. total {0}".format(balance))

       elif money2 + commission <= balance:
          break
       
    print("{0} has been withdraw.".format(money2))
    print("{0} is the commission. total {1}".\
          format(commission, balance - commission - money2))
    return balance - commission - money2

balance = int(input("how much to start with? "))

while True:  
  balance = deposit()
  balance = withdraw()
  if balance == 0:
    break
harivin
  • 15
  • 2
  • I would turn the line into a function (see https://stackoverflow.com/questions/5996881/how-to-limit-a-number-to-be-within-a-specified-range-python) – Tom McLean Jun 07 '23 at 07:49

2 Answers2

1

It's best to separate and main computation of the commission fee and the min/max range. Because having all in one line just makes it hard to read. Also use some good variable names, and constants with names instead of magic values.

def clip(value: float, min_value: float, max_value: float) -> float:
    assert min_value < max_value
    return max(min(value, max_value), min_value)

# and later during the computation
MIN_COMMISSION_FEE = 50
MAX_COMMISSION_FEE = 100

raw_commission = money2 * (10 / 100)
commission = clip(raw_commission, MIN_COMMISSION_FEE, MAX_COMMISSION_FEE)

clip is a typical name for such an operation. E.g. Numpy has an operation with the same name and functionality: https://numpy.org/doc/stable/reference/generated/numpy.clip.html


And as a side note, don't trust everything from pylint. It is very opinionated, and a lot of the default rules are somewhat questionable (at least for me).

Jakube
  • 3,353
  • 3
  • 23
  • 40
  • thanks for the detailed comment sir!, not quite understanding all of it but I'll try and study. was a great help indeed – harivin Jun 07 '23 at 07:57
0

With plain python, the code you provided is as concise as it gets. If you use min/max or if-statements comes down to preference.

If you would like to bother with mathematical modules, you could use numpy.clip which does exactly what you want.

import numpy as np
np.clip(YOUR_VALUE, 50, 100) # returns a value between 50 and 100
Quantum
  • 510
  • 1
  • 2
  • 19