Python Forum
Just starting out, allready stuck..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Just starting out, allready stuck..
#1
Hi.

I'm not really a student, but i sort of made it a new year's resolution to learn how to code. This seemed like the appropriate place to ask my question. I apologize if it's not.

I started reading a book on learning python and i'm allready stuck on one of the first exercises :/

So the task involves the change direcory command. I'm supposed to write these commands in a terminal:

C:\> cd Desktop\python_work
C:\Desktop\python_work> dir
hello_world.py
C:\Desktop\python_work> python hello_world.py

After the first line i always get this message: SyntaxError: unexpected character after line continuation character

To explain: i had to write a simple line of code so that the message 'Hello Python world!' would be printed and save it in Geany as 'hello_world.py'. The commands above should be written in the terminal to run this code.

I hope i've given enough information to help me clarify what's amiss here. I'm serious about learning to code and it's kinda frustrating to be stuck right at te start :s

Python version: Python 3.6.4
OS: Windows 7
Reply
#2
You need to post your code so we can see what is going on. Be sure to format it (with the little python button in the toolbar)
Reply
#3
there's really not much to the code, it's a simple print command

print("Hello Python world!")
it's trying to run it from the terminal with the instructions from the book where things go wrong
Reply
#4
We need to check which version of python you are running. Just type python and let us know what you get as a version (to exit the shell, type exit()).
(I know that you mentioned the python version in your first post, it is just to double-check it...)

Also, verify that there are no "invisible" characters in your hello_word.py file after the last parenthesis. Position your cursor after it and press the Delete key many times to be sure.
Reply
#5
Such things may happen if you copy and paste code from certain files such as pdf files to your editor. These files contain invisible characters that produce invalid python code (this is not a python specific issue).

You can delete the whole file and type it by hand. In fact there is a well known online tutorial named "Learn python the hard way". Its main paradigm is to type every single character of every program in the tutorial! No copy and paste is allowed.
Reply
#6
In addition to the already mentioned advice, make sure, when you are writing your code that you do NOT use a 'formatting' text editor such as MS Write or MS Word. These editors also insert 'hidden' formatting characters. Instead, use a plain text editor like Notepad or a better option is the free Notepad++ (with the Python plugin). In addition, do not mix tabs and spaces, in Python always use 4 spaces for indentation.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
(Jan-06-2018, 11:12 AM)squenson Wrote: We need to check which version of python you are running. Just type python and let us know what you get as a version (to exit the shell, type exit()).
(I know that you mentioned the python version in your first post, it is just to double-check it...)

Also, verify that there are no "invisible" characters in your hello_word.py file after the last parenthesis. Position your cursor after it and press the Delete key many times to be sure.
Python version 3.6.4 (v3.6.4:d48eceb). I thought your advice on deleting possible white spaces would work, but alas :(

(Jan-06-2018, 12:15 PM)Gribouillis Wrote: Such things may happen if you copy and paste code from certain files such as pdf files to your editor. These files contain invisible characters that produce invalid python code (this is not a python specific issue).

You can delete the whole file and type it by hand. In fact there is a well known online tutorial named "Learn python the hard way". Its main paradigm is to type every single character of every program in the tutorial! No copy and paste is allowed.
i typed it myself, didn't copy/paste. i even tried to copy/paste before, but it didn't do anything :D

(Jan-06-2018, 01:30 PM)sparkz_alot Wrote: In addition to the already mentioned advice, make sure, when you are writing your code that you do NOT use a 'formatting' text editor such as MS Write or MS Word. These editors also insert 'hidden' formatting characters. Instead, use a plain text editor like Notepad or a better option is the free Notepad++ (with the Python plugin). In addition, do not mix tabs and spaces, in Python always use 4 spaces for indentation.
nope. i used Geany. supposedly this is a helpful tool to write your code in.

i'll add the message i'm getting in python tags

>>> C:\> cd Desktop\python_work
  File "<stdin>", line 1
     C:\> cd Desktop\python_work
                               ^
SyntaxError: unexpected character after line continuation character
does the little '^' under the last k mean anything..?
Reply
#8
You must type the cd Desktop\python_work statement (don't type C:\, this is the prompt) and the python hello_world.py command at the prompt level. You don't need to be inside the python shell.
Reply
#9
(Jan-06-2018, 07:10 PM)squenson Wrote: ...command at the prompt level. You don't need to be inside the python shell.
i don't understand what this means
Reply
#10
It won't work in the python shell. The cd command works in a terminal/console. But Geany has probably a keyboard shortcut to run the program.
Reply


Forum Jump:

User Panel Messages

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