0

I designed an input
A python program based on the sum of deposits for each day within 7 days
I hope the execution result is:

Please enter the deposit on day 1: 40
Please enter the deposit on day 2: 70
Please enter the deposit on day 3: 89
Please enter the 4th day deposit: 489
Please enter the 5th day deposit: 39
Please enter the deposit on day 6: 48
Please enter the 7th day deposit: 99
   Total deposit: 874 dollar

But the problem with my python program is
Circulate from the deposit on the first day to after the deposit on the seventh day
Unable to add up smoothly, but re-start the cycle from the first day of deposit
In an infinite loop
My code:

sum1=0
mes=list()
while True:
   for mes in range(1,8):
      mey=int(input(f"Please enter the deposit on day {mes}:"))
      if mes==7:
       break
       mes.append(mey)
       sum1+=mey
print("Total Deposit",str(mes),"dollar")
4
  • 1
    You should break again after the for loop. Commented Dec 4, 2020 at 7:56
  • For clarity do you want the total after entering the 7 days then ask for another 7 days before giving another total? Commented Dec 4, 2020 at 7:56
  • I want me to enter every day's deposit (7 days in total) After the input, the loop is interrupted Calculate the total deposit Commented Dec 4, 2020 at 8:08
  • 1
    You have two loops here. The for loop and the while loop. You have only one break statement. The break statement exits the for loop. Then goes back to the while loop and starts all over again. The code you have posted and the output you shared are not the same. Day 4, 5, and 7 code looks different. Also, why are you checking for mes==7, the for loop will exit once mes reaches 8. In summary, you just need the for loop. You can remove the while loop. Commented Dec 5, 2020 at 0:05

2 Answers 2

2
total = 0
mes = []

for i in range(1,8):
    m = int(input(f"Please enter the deposit on day {i}: "))
    mes.append(m)
    total += m

print(f"Total Deposit {total} dollar.")
Sign up to request clarification or add additional context in comments.

Comments

2

Here are some problems I can see:

  • You are re-declaring mes variable in the for loop; use another variable name
  • the while loop is unnecessary
  • the break is unnecessary, because the for loop will execute while mes <= 7 (in this case the break will exit the for loop, and not the while loop)
  • you should print the sum1 variable at the end; the str() function is unnecessary

Your new code should look like this if we address the code issues as stated above.

sum1=0
mes=list()
for i in range(1,8):
    mey=int(input(f"Please enter the deposit on day {mes}:"))
    mes.append(mey)
    sum1+=mey
print("Total Deposit is",sum1,"dollar")

Comments

Your Answer

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