Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output in for LOOP iterated
#1
Hi guys,
I am a beginner in Python and I am using PyScripter. Can someone help me why in this simple for loop example my output result is iterated. I expect only Total=80

prices=[10,20,50]
total=0
for item in prices:
        total= total + item
        print(f"Total:  {total}")
Error:
>>> Total: 10 Total: 30 Total: 80 >>>
Reply
#2
Correct the indentation. Print is currently part of the for-loop body. It should be after and outside the for-loop.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The way to chase how the set of integer is iterated in mutating loop khei4 4 786 May-26-2023, 09:17 AM
Last Post: Gribouillis
  using 'while loop' output going into infinite loop... amitkb 2 1,908 Oct-05-2020, 09:18 PM
Last Post: micseydel
  sports Stats > table output loop problems paulfearn100 3 2,444 Jul-22-2020, 03:21 AM
Last Post: c_rutherford
  Print output in single file using pramika loop deepakkhw 1 2,036 Jul-11-2020, 11:57 AM
Last Post: j.crater
  output list reducing each time through loop 3Pinter 6 3,443 Mar-19-2019, 01:31 PM
Last Post: perfringo
  Loops - new terminal output to file for each loop jm_ice 1 2,552 Dec-21-2018, 02:42 PM
Last Post: ichabod801
  A little unexpected output from a while loop linuxnoob 3 3,077 Jul-17-2018, 01:02 AM
Last Post: gontajones

Forum Jump:

User Panel Messages

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