Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop
#1
Hi,

I have a for loop program for which want result.

Program:
prices = [10,20,30]
total = 0
for price in prices:
    total += price
    print(f"Total: {total}")
Result:
Output:
Total: 10 Total: 30 Total: 60
Is it possible to get result as Total : 60; I dont want Total: 10 & Total: 30 to appear.
Yoriz write Nov-02-2022, 09:01 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

Thumbnail(s)
   
Reply
#2
At the moment the print happens inside the loop, think about how you could alter the code to make the print happen after the loop is finished.
Hint: indentation is key to what happens inside the loop.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020