1
votes

I have Python 3.8(32-bit) installed, and I am using Atom to write my attempts and then copy-pasting them into the Python terminal.

The following code is copied directly from the very beginning of an introductory Python course I am taking "for fun":

n = 5
while n > 0:
  print(n)
  n=n-1
print('Blastoff!')

The code works in every sandbox I can find, and the last line works on its own in my terminal. But when I copy it to my terminal, I get an invalid syntax error that points to the word print. I can fix this and get the desired output by changing my code to:

n = 5
while n > 0:
  print(n)
  n=n-1
else:
    print('Blastoff!')

But I have three issues with this:

  1. Why does my original code not work, as it is copied directly from the course?
  2. I need to hit Enter twice after copying in that second block of code for it to run. Why is that?
  3. Why does Atom insist on indenting the last print farther than my other indents?

Here is what I am seeing when entering my first code brick:

>>> n = 5
>>> while n > 0:
...     print(n)
...     n=n-1
... print('Blastoff!')
  File "<stdin>", line 4
    print('Blastoff!')
    ^
SyntaxError: invalid syntax
1
Are you mixing spaces and tabs for indentation? That's my first instinct.G. Anderson
Please edit the question to show the full error message (traceback) as properly formatted text. The common indentation used in Python is four spaces. Atom seems just to follow this convention.Michael Butscher
@MichaelButscher n00b problems: I'm not sure what that all means. but if you explain it, I'd be happy to do it!The Count
Just copy and paste the error message (which usually starts with "Traceback") in the question. Properly formatted means the code formatting here to ensure that the line breaks are preserved.Michael Butscher
Great. If you're already inside the python interpreter, you can write quit() to exit the interpreter and you'll be back inside the windows command prompt. If you've already exited the python interpreter, you can write python blastoff.py in the windows command prompt, and it will run your file. Let me know if it's not clear the distinction between the windows command prompt and the python interpreter.hkennyv

1 Answers

1
votes

Since you're entering code into the Python interpreter, it'll interpret the code line-by-line. This is great for quick tests and checks, but for larger code, you'll want to run an entire file.

You can achieve this a couple different ways:

  1. Running it from the command prompt/terminal. If your Python executable is in your PATH, you can open a command prompt and navigate to your file and run python myfile.py. See "How to add Python to Windows PATH".
  2. If you've installed Python from python.org, you may have IDLE installed. You can run the IDLE application and open your file from the menu File > Open. From there, you can run the file from the menu Run > Run Module.

I'd suggest the second option since you are learning and it will help you focus on coding instead of fighting your code environment. However, feel free to revisit option #1 in the future. It is definitely helpful to know your way around the command line (if you work on a machine without IDLE installed, this would be the proper way to run Python files).

Also "How to Run Your Python Scripts" is a great resource for learning more about how running scripts in Python works.