0
#Variables
total = 0
day = 0
car = 0

while day <= 4:
  day += 1

  if day <= 5:
    print('Cars sold on day', day, end = ': ',)
    carSold = int(input(''))

    for amount in range(carSold):
      print('Selling price of car', amount + 1, end = ':\t')
      price = int(input('$'))

  if day >= 5:
    total += price
    car += carSold
    print('\nYou sold', car, 'cars for a total of $', format(total,',.2f'))

The different configurations I have tried all either add after every input or just the last value.

0

2 Answers 2

1

You added values to total and car only at the last day, while the code below is adding values everyday.

#Variables
total = 0
car = 0

for day in range(1, 6):
    carSold = int(input('Cars sold on day {}: '.format(day)))
    car += carSold

    for amount in range(carSold):
        total += int(input('Selling price of car {}: $'.format(amount+1)))

    if day == 5:
        print('\nYou sold', car, 'cars for a total of $', format(total,',.2f'))
Sign up to request clarification or add additional context in comments.

2 Comments

You are the bomb.com. Can you explain what the format for day and amount+1 does?
0

EDITED: Writing concise code is very important in Python, and so is error checking since it is very easy to spawn an error by typing anything other than a number:

total = 0
day = 0
car = 0
carSold = 0
lastsold = 0


while day <= 4:
    day += 1
    try:
        if day <= 5:
            lastsold += int(input('Cars sold on day %i: '%day))
            carSold += lastsold

        for amount in range(carSold):
            total += int(input('Selling price of car %i: $'%(amount+1)))
    except:
        day -= 1
        carSold -= lastsold
        continue
    lastsold = 0

    if day >= 5:
        print('\nYou sold %i cars for a total of $%.2f'%(car,total))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.