I'm practicing the item calculation code.

Asked 2 years ago, Updated 2 years ago, 51 views

Here's the condition.

(The price of the item is given in advance.)

So the code I wrote now is

## Current amount
pres_money=5000 
## the price of goods
goods_price = {'apple':1500, 'egg':2000, 'mackerel':3300, 'galbi':4000}
goods_list=[]

## Code
while(1):
    a = input ('Enter what you want (among apples, eggs, mackerel, ribs):')
    If 'apple' or 'egg' or 'mackerel' or 'galbi' in a:
        print('current balance:', pres_money -= goods_price.get(a))
        print( goods_list.append(a) )
        if pres_money == 0:
            break
    else:
        continue

I did this, but the assignment operator didn't work What's the problem? Is there any other way to subtract 1,500 won from 5,000 won and 2,000 won from the remembered 3,500 won?

function python

2022-09-22 10:46

1 Answers

Analyze the example below.

You don't have to subtract every time, just check if the values of goods_sum_price are greater than pres_money.

pres_money=5000
goods_price = {'apple':1500, 'egg':2000, 'mackerel':3300, 'galbi':4000}

goods_list = ['Apple', 'Mackerel'] # Assume that you received two products.
goods_sum_price = sum(goods_price[goods]for goods in_list) # Total entered product value

goods_sum_price 
Out[6]: 4800

pres_money > goods_sum_price 
Out[7]: True


2022-09-22 10:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.