Python Forum
Python Shell 3.9.0 - Issue with indentation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Shell 3.9.0 - Issue with indentation
#11
NEITHER of the 3 snippets you show above can possibly work on ANY python version/iterpreter! They have the same problem. The indentation of line 3 is INCONSISTENT with the rest of the indentation of the code. In addition the indentation of the last line in the third snippet is off too. The error you get is pretty clear too.
for i in [12, 16, 17, 24, 29]:
    if i % 2 == 1: # 4 spaces indentation = 1 level deep
       break # 7 spaces indentation, when it has to be 8 spaces to represent 2 levels deep indentation
    print(i) # 4 spaces indentation = 1 level deep
  
print("done") 
this will work

for i in [12, 16, 17, 24, 29]:
    if i % 2 == 1: # 4 spaces indentation = 1 level deep
        break # 8 spaces indentation = 2 levels deep indentation
    print(i) # 4 spaces indentation = 1 level deep
  
print("done") 
If you are not reading and following what you have been told or what the error says, no one can help you though.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#12
The 3-space third line indentation is non-standard, but it should work just fine.

The only one that is an error for me is the final one. Line 5 isn't a new indentation level, and the single-space indentation doesn't match any previous level, so it's invalid. Of course that has nothing to do with 3.9. That's an error on previous versions as well.
Reply
#13
(Oct-26-2020, 04:51 AM)Earis Wrote: In conclusion am I to assume that the above code will never work on python Shell 3.9.0 ?
When you run in IDLE Python shell you now that you most do File ➡ New Window the write code in this Window and save with .py extension and run with F5(Run Module)?
As i posted in #3.
Shell start in inter activate mode >>>(Enter after each line) if paste in code there will get error as you have gotten.
Reply
#14
Typing this in the shell works:
>>>print('Done')
Typing this in the shell also works:
>>> for i in [12, 16, 17, 24, 29]:
...    if i % 2 == 1:
...       break
...    print(i)
...
Notice that the extra empty line is required before Python accepts this as a statement that can be evaluated.

You cannot do this
>>> for i in [12, 16, 17, 24, 29]:
...    if i % 2 == 1:
...       break
...    print(i)
... print('Done')
The reason is that Python is still looking for the end of the for loop when it encounters print('Done'). You can argue that Python should accept this as the end of the previous statement, evaluate for loop and then evaluate the print('Done'), but that is not how the shell interpreter is written. The shell interpreter appears to only work one statement at a time.

As has been mentioned, you also cannot copy and paste this:
for i in [12, 16, 17, 24, 29]:
   if i % 2 == 1:
      break
   print(i)

print('Done')
The interpreter appears to take anything pasted as a single statement. The code above is two statements,

If you really want to type in the shell remember the one statement at a time. When typing loops or conditionals press enter on an empty line to indicate the end of the statement.

If you really want to type more complex python, how about putting the code in a file and using exec(open(filename).read())? Or better yet define your own version of Python2 execfile
>>> def execfile(filename):
...     exec(open(filename).read())
HLD202 likes this post
Reply
#15
>>>for i in [12, 16, 17, 24, 26]
...    if i % 2 == 1:
...        break
...    print(i)
...print("done")

#you can not do this, because you can not use syntax or etc. at the end of loop 
#you have to close the loop by pressing enter at the end and then type the rest of code
#like this:

>>>for i in [12, 16, 17, 24, 26]
...    if i % 2 == 1:
...        break
...    print(i)
...
>>>print("done") 


here's the result:

>>>for i in [12, 16, 17, 24, 26]
...    if i % 2 == 1:
...        break
...    print(i)
...
12
16
>>>print("done")
done
Reply
#16
Thank you for your replies and recommendations

.

I have included a video with all the solutions mentioned so that we are on the same page.
Reply
#17
(Oct-30-2020, 06:13 AM)Earis Wrote: I have included a video with all the solutions mentioned so that we are on the same page.
As i expected you only do this in interactive shell >>>
You should not paste code with many line into interactive shell >>>.
When >>> should write one line and then Enter,
and not paste in code with many lines,then get error as you have gotten.

When write scripts with many lines as you do now,as i have posted a couple times of time,
start with File ➡ New Window then write/paste code in this Window and save with .py extension and run with F5(Run Module).

Most other Editors do not start in interactive shell,but a windows where you write code and save code with .py extension.
IDLE is a choice that most that been doing Python for while do not like or use at all.

So a couple of easy Editor that replace IDLE as you on Windows is PyScripter and Thonny.
Both of this have a Window where you write code visible and interactive shell >>> in bootom,as see in older image that i made.
[Image: UtbcqZ.jpg]

Also look at VS Code,a lot Python users use it look at Python extension 26-mil+ downloads.
VS Code from start
A other one of the big editors is PyCharm Free Community.
buran likes this post
Reply
#18
Thank you !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make a Python program run in a dos shell (cmd) Pedroski55 2 564 Nov-09-2020, 10:17 AM
Last Post: DeaD_EyE
  Python "Terminal" vs "Shell" SectionProperties 2 1,032 Apr-10-2020, 08:36 AM
Last Post: SectionProperties
  looping and indentation issue ameydiwanji 3 941 Jul-01-2019, 10:53 AM
Last Post: perfringo
  python shell teelado 1 883 Jun-13-2019, 01:49 AM
Last Post: Larz60+
  Is it possible to have my python shell change the backgrounds colour? OTO1012 2 1,639 Mar-07-2019, 09:32 PM
Last Post: woooee
  Indentation error in Python code ErnestTBass 5 1,599 Feb-28-2019, 04:26 PM
Last Post: samsonite
  running python script from shell invoked with os.system("x-terminal-emulator -e /bin/ markhaus 2 1,427 Feb-21-2019, 11:55 PM
Last Post: markhaus
  python shell elabroting output kapilan15 2 1,108 Feb-11-2019, 07:09 PM
Last Post: snippsat
  Need shell for python 3.6 kaiba 6 1,698 Oct-28-2018, 01:36 PM
Last Post: kaiba
  using a shell script within python Krszt 2 1,462 Oct-10-2018, 11:00 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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