Python Forum
Common understanding of output processing with conditional statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Common understanding of output processing with conditional statement
#1
Question 
Hello,

Can anyone please explain why the output calls the Evaluation statement before printing the series?

And what is the process to print the evaluation at the end?

Thank you. Heart

----------------------------------------------
i=2000
while i <= 2100:
    i= i + 10
    print(i)
    if i == 2100:        
        print('task complete')
----------------------------------------------

Output image-    
Gribouillis write Sep-17-2023, 07:13 AM:
Please post all code, output and errors (it it's 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.
Reply
#2
The problem is that the program prints 'task complete' before the task is completed, because when i == 2100, the loop must run one more time because the loop says while i <= 2100.

Anyway, the 'task complete' thing should come after the loop.
i = 2000
while i <= 2100:
    i= i + 10
    print(i)
print('task complete')
neail likes this post
Reply
#3
Could throw a break statement in there.
i = 2000
while i <= 2100:
    i= i + 10
    print(i)
    if i == 2100:   
        print('task complete')
        break
Pedroski55 and neail like this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
A for loop is cleaner when you know exactly how many times the loop executes.
for i in range(2000, 2101, 10):
    print(i)
print('task complete')
Reply
#5
@Gribouillis

Thank you, I have tried multiple combinations before posting it here, but don't know how I missed that, I feel so dumb Tongue

update-
I just noticed you omitted the == operator. I know its silly I am instructing and comparing the exact value. but can you do that with the == operator?
Reply
#6
@menator01
hi, is the break operation really necessary? because it's already stopping, I mean its not an infinite loop.

update-

kudos, problem solved.
What magic is the break doing?? what did I miss? Huh
Reply
#7
@deanhystad

hi, I have learned the range (from, to, interval) function, but this was an experiment, and despite trying a many combination, I failed to get the desired result so I posted. actually I wanted to clear my conception. Still, thank you, if someone is looking will be helpful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with output from if statement fgaascht 6 1,963 Jan-17-2022, 02:53 PM
Last Post: perfringo
  Help understanding RegEx logic/output pyNewbee 4 2,293 Nov-15-2020, 02:21 AM
Last Post: pyNewbee
  need help in conditional statement if else kambohg 3 1,927 Mar-27-2020, 12:27 AM
Last Post: kambohg
  Calculation using output of if statement StephenBeckman 3 2,015 Feb-07-2020, 10:19 PM
Last Post: jefsummers
  Conditional not processing as intended (Colt Steeleā€™s Udemy course) Drone4four 3 2,009 Nov-07-2019, 04:47 PM
Last Post: Drone4four
  Passing print output into another print statement Pleiades 6 3,159 Sep-08-2019, 02:37 PM
Last Post: Pleiades
  Unexpected output: if statement CabbageMan 1 1,770 Sep-04-2019, 04:12 PM
Last Post: ThomasL
  understanding output of bytes/raw data rootVIII 3 2,780 Aug-01-2019, 01:00 PM
Last Post: rootVIII
  Trying to code backwords in if statement for different output in some scenarios skrivver99 1 2,446 Dec-03-2018, 01:32 AM
Last Post: Windspar
  Understanding "help()" output? Athenaeum 4 3,921 Sep-29-2017, 09:47 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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