0

I have a dynamically generated dictionary. Example:

kwargs = { 'name':'Rafi', 'amount': 530}

This dictionary will be used as function parameter. But in case I need to change the value of kwargs['amount'] often (not always).

def printer(**kwargs):
    print(kwargs)

for taka in range(100,kwargs['amount'],100):
    printer(**kwargs) #Each time (5 time) Output should be: { 'name':'Rafi', 'amount': 100}
    kwargs['amount']-=100

printer(**kwargs) #Output should be: { 'name':'Rafi', 'amount': 30}

It is not possible to change the printer(**kwargs) function. I have to do it through for loop.

I need to make some data entry, where until the balance is more than 100 ( kwargs['amount'] > 100 ), every times $100 will be recorded.

Kindly, help me to figure it out. (Working with django.)

  • `kwargs['amount'] if kwargs['amount'] <= 100 else 100` – wwii Jul 04 '20 at 17:12
  • Can you clarify what you have tried to or why you expect to already have ``kwargs['amount']`` clamped to 100? It would also help to know in far you feel clamping an integer is related to kwargs – the two are orthogonal. Do you need some logic (that makes both clamping and kwargs relevant) not shown in the question? – MisterMiyagi Jul 04 '20 at 17:33
  • @wwii Sorry, But I'm unable to apply, what you said. Can you give me the code in more detail please(in my program's way)? Just for clarifying, kwargs['amount']=100 just for entry, as entry is $100. –  Jul 04 '20 at 17:46
  • Are you asking how to print an integer (`100`) as a string with a dollar sign in front of it? - [Currency formatting in Python](https://stackoverflow.com/questions/320929/currency-formatting-in-python) – wwii Jul 04 '20 at 18:10
  • @wwii No! I used $ to indicate money. It's nothing –  Jul 04 '20 at 18:23

1 Answers1

1

A bit of naive approach but try this :

kwargs = { 'name':'Rafi', 'amount': 530}
  
def printer(**kwargs):
    if kwargs['amount'] <= 100:
        print(kwargs)
    else:
        kwargs_dummy = kwargs
        kwargs_dummy['amount'] = 100
        print(kwargs_dummy)

for taka in range(100,kwargs['amount'],100):
    printer(**kwargs) 
    kwargs['amount']-=100

printer(**kwargs) 
Parth Choksi
  • 166
  • 8
  • I can't edit the `printer(**kwargs)` .The Code is far more complex. Actually, it is related to django model. –  Jul 04 '20 at 18:07