how do I make the command outside of while loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how do I make the command outside of while loop

I can get the loop to run fine but when I add print("finished") I get a syntax error. yes I am aware the print("finish") has to be outside the while loop but how do I do that?

26th Aug 2016, 4:56 PM
Billy Bonney
Billy Bonney - avatar
17 Answers
+ 1
removing the spaces didn't work either
26th Aug 2016, 6:13 PM
Billy Bonney
Billy Bonney - avatar
0
check for the indentation. Put the print statement at the same indentation as of the while. eg. while <condition>: do something do something else print("finished")
26th Aug 2016, 5:39 PM
Shashi Shekhar
0
Remove 4 spaces from the beginning of lines. 😊 Example while condition: do something print("finish")
26th Aug 2016, 5:51 PM
Dinanath Basumatary
Dinanath Basumatary - avatar
0
I = 1 while I <= 5: print(I) I = I + 1 print("finished") this is exactly how I have it and I always get a print("finish") syntax error
26th Aug 2016, 5:53 PM
Billy Bonney
Billy Bonney - avatar
0
Can you paste the exact error please.
26th Aug 2016, 5:55 PM
Dinanath Basumatary
Dinanath Basumatary - avatar
0
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> i = 1 >>> while i <= 5: ... print(i) ... i = i + 1 ... ... print("Finished") File "<stdin>", line 5 print("Finished") SyntaxError: invalid syntax #everything is lined up correctly. this was cut and paste and didn't turn out well
26th Aug 2016, 6:12 PM
Billy Bonney
Billy Bonney - avatar
0
can you put it in a file and run ? It looks like the indentation is not correct when you run on the console. i tried the same code and it works fine. You can also try putting the same in the code playground. it works fine there as well
26th Aug 2016, 6:26 PM
Shashi Shekhar
0
i=0 while(i<=5): <Tab>print(i) <Tab>i=i+1 print("finished") <Tab> means entre Tab key :)
26th Aug 2016, 6:40 PM
Varun Vh
Varun Vh - avatar
0
hadn't tried that way but just did same error. I can get it to work in playground just not the Python 3.5.2 on my computer
26th Aug 2016, 6:49 PM
Billy Bonney
Billy Bonney - avatar
0
By "Indentation not correct" i mean, if you look at the code you pasted with the error, you can see that print is being considered as part of the while loop itself, rather than the as a separate statement. you can the difference if you compare the first line and the last line.
26th Aug 2016, 6:54 PM
Shashi Shekhar
0
I think that's my input error from cut and paste. the w and p are lined up directly inline. no indentation on print("finish") on my computer
26th Aug 2016, 6:58 PM
Billy Bonney
Billy Bonney - avatar
0
and if it is and indentation issue how do I fix it it
26th Aug 2016, 6:59 PM
Billy Bonney
Billy Bonney - avatar
0
in console, it doesnot identify the "print('finished')" statement as a next line but as part of the while loop. that is why you get the error. try creating a method around the code. eg def fun(): i=0 while(i<=5): print(i) i=i+1 print("finished") fun()
26th Aug 2016, 7:33 PM
Shashi Shekhar
0
now I'm trying it in Python shell. how do I breakout the indentation with out running the loop
26th Aug 2016, 7:40 PM
Billy Bonney
Billy Bonney - avatar
0
that method worked. is there a different interface I should be using as a newb? I really appreciate your help
26th Aug 2016, 7:45 PM
Billy Bonney
Billy Bonney - avatar
0
it would be lot easier if you use script if you are working with multi line code. Python console is good for running single line commands but can land you into issues if the code size increase. Python is an interpreter based language, that is, it executes line by line instead of processing the code as a whole. Which means as soon as you hit enter the code is processed. To run any cone through files you can save the file with .py extension and then run as "python filename.py" from the console by going to the location where you saved it
26th Aug 2016, 7:56 PM
Shashi Shekhar
- 1
when you say the indentation isn't correct where are you talking about. I also got it to run fine in the playground but not in IDLE
26th Aug 2016, 6:37 PM
Billy Bonney
Billy Bonney - avatar