0

Codecoach challenge

''' You are shopping at a store that is having a special where you do not have to pay the tax on anything that costs 20 dollars or more! If you have a list of prices for all of your items, what is your total once the tax is added in? Tax is 7% on the items that you would still need to pay tax on. Task: Determine the total cost once you include tax of 7% on the items that are still taxed. Input Format: A string of numbers, separated by commas, that represent to price of each item that you are going to buy. Output Format: A number, rounded to two decimal places, of the total for your purchase once tax is included on items under 20 dollars. ''' input = input().split(",") prices = list(map(int,input)) newList = [] for i in prices: if i <=20: i *= 1.07 newList.append(i) else: newList.append(i) total_cost = round(sum(newList),2) print (total_cost) Suggest me a better way to shorten this code. I have written newList.append(I) twice and it's annoying me. Thanks.

29th Dec 2020, 7:59 AM
CHANDAN ROY
CHANDAN ROY - avatar
2 Respuestas
+ 1
for i in prices: if i < 20: i *= 1.07 newList.append(i) Instead of appending to new list you could add up directly in the loop
29th Dec 2020, 8:22 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
does this works ? its been a while since the last time i use python i might mixed things up newList.append(i*1.07 if i <= 20 else i)
29th Dec 2020, 8:24 AM
Rei
Rei - avatar