Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem With Tutorial
#1
I'm trying to learn Python as I will need to be able to use it for a project I'm working on. I'm using an online tutorial to teach myself about it. In the tutorial, it gives me the following code:

dogs = ['border collie', 'australian cattle dog', 'labrador retriever']

for dog in dogs:
    print('I like ' + dog + 's.')
    print('No, I really really like ' + dog +'s!\n')
    
print("\nThat's just how I feel about dogs.")
Which is supposed to produce:

Quote:I like border collies.
No, I really really like border collies!

I like australian cattle dogs.
No, I really really like australian cattle dogs!

I like labrador retrievers.
No, I really really like labrador retrievers!


That's just how I feel about dogs.

However, whenever I enter it I get the following error:

Error:
File "<stdin>", line 5 print("\nThat's just how I feel about dogs.") ^ SyntaxError: invalid syntax
As I understand it, the lack of an indentation on the last line is what makes that line only run once (and not once per item in the list). When I add an indent, it tells me that there's an unexpected indent (as I expect), so that doesn't seem to be the issue. So I have no idea what the syntax issue with the last line is. Can anyone help?

I'm running this in Python 2.7.10. The computer is a Mac.
Reply
#2
can you post the full code, exactly as you entered it? sometimes the error is on the previous line.
By the way, you should be using python3. python2 support ended in the beginning of 2020
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
#3
1. Use python 3, python 2 isn't supported anymore.
2. Even if you do use python 2, brackets aren't supported in python 2 in print statements. You can change that, but I would recommend you to switch to python 3 - python 2 is no longer supported. The code works just fine in python 3
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
I installed Python 3.8.3 and I'm still getting the same error. What I posted in the OP is exactly what I pasted into the terminal. I tried typing each line in manually and it executes after the second blank line. When I typed it in manually without the two blank lines:

dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
for dog in dogs:
    print('I like ' + dog + 's.')
    print('No, I really really like ' + dog +'s!\n')
print("\nThat's just how I feel about dogs.")
I got the same error message as before.
Reply
#5
While you can type programs directly into the interactive interpreter, it's often easier to write your program to a file, then pass the file to python from the shell. You don't have to type it in each time, and if you fix typos, they shouldn't come back.

The program that you've pasted above doesn't produce the error you've mentioned. Is there any chance there's a typo somewhere?

If you copy the code above, paste it into a file, then run python on that file, you shouldn't get the error.

$ python3 dogs.py

Output:
I like border collies. No, I really really like border collies! I like australian cattle dogs. No, I really really like australian cattle dogs! I like labrador retrievers. No, I really really like labrador retrievers! That's just how I feel about dogs.
Reply
#6
maybe this is one of the rear cases when screenshot of your code and error is worth it. There is something you are not doing exactly right. Also as @bowlofred advised - don't use interactive shell, but write the code in a file
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
#7
Also, make sure you are actually using Python 3.8. You installed it, but both that and 2.7 are on your system if you did not uninstall 2.7. As a result, you may still be using 2.7. Suggest you do the following:
import sys
print(sys.version)
Reply
#8
When I put it into a .py file it worked, so my problem is solved. I did check that I'm running Python 3.

Here's a screenshot of the code entered directly in the terminal, if you can tell what I did wrong originally:

[Image: veBpP14]
Reply
#9
Wild guess. How did you get the last print statement de-indented? Proper would be to use the backspace key to delete the tab. You would get that exact error if you use the back arrow key instead.
Reply
#10
(Jun-15-2020, 06:14 PM)BeginningCoder Wrote: entered directly in the terminal, if you can tell what I did wrong originally:

>>> dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
>>>
>>> for dog in dogs:
...     print('I like ' + dog + 's.')
...     print('No, I really really like ' + dog +'s!\n')
...
[... print("\nThat's just how I feel about dogs.")
I really don't see how you managed to enter line # 7 with the [ that can be seen at the start of the line 7
After hitting enter on line #6 (empty line) the loop should be executed. and both prints (lines 4 and 5) executed for each of the dogs.

>>> dogs = ['border collie', 'australian cattle dog', 'labrador retriever']
>>> 
>>> for dog in dogs:
...     print('I like ' + dog + 's.')
...     print('No, I really really like ' + dog +'s!\n')
... 
I like border collies.
No, I really really like border collies!

I like australian cattle dogs.
No, I really really like australian cattle dogs!

I like labrador retrievers.
No, I really really like labrador retrievers!

>>> 
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


Forum Jump:

User Panel Messages

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