Unit 3: Example code from kernel is not working. Bug?

In Python for Robotics Unit 3: Conditional statements and Loops, the examples in the cell being used is not working. In this Unit you are expected to create a python script under a folder and fill the script with the code provided from a cell in the kernel, shown below:

movie = input("What’s your favorite move? ")

if movie == “Avengers Endgame”:
print(“Good choice!”)

Then you are asked to execute the file.
Pretty simple and straight forward, right?
The dialog in the kernel goes on expressing as if the code worked, and provides a picture of what it should look like when executed correctly.
When I copy and pasted the code provided into the script and ran it, it came up with an error. I thought, it must of been something I did. I will try fixing it! I spent nearly 20 minutes experimenting trying to make this simple code work and even completely deleting the file and recreating it just in case. I went back into the kernel and ran the cell that provided the code, and the cell came up with an error, just like mine did in the shell. Here is the error that was stated from provided code in the cell:
What’s your favorite movie? Avengers Endgame
File “”, line 1
Avengers Endgame
^
SyntaxError: unexpected EOF while parsing

I have copied Avengers Endgame directly from the code and pasted it for input about a dozen and one times thinking I must of just been typing the movie wrong. This error still shows up.
I have looked through a lot of examples and ran them in the cell and most of them come back with an immediate error.

More example cells having errors:
1.

movie = input("What’s your favorite movie? ")

if movie == “Avengers Endgame” or movie == “Titanic”:
print(“Good choice!”)

error:
What’s your favorite movie? Titanic

NameErrorTraceback (most recent call last)
in ()
----> 1 movie = input("What’s your favorite movie? ")
2
3 if movie == “Avengers Endgame” or movie == “Titanic”:
4 print(“Good choice!”)

/etc/jupyter/lib/python2.7/site-packages/ipykernel/ipkernel.pyc in (prompt)
174 self._sys_eval_input = builtin_mod.input
175 builtin_mod.raw_input = self.raw_input
–> 176 builtin_mod.input = lambda prompt=’’: eval(self.raw_input(prompt))
177 self._save_getpass = getpass.getpass
178 getpass.getpass = self.getpass

/etc/jupyter/lib/python2.7/site-packages/ipykernel/ipkernel.pyc in ()

NameError: name ‘Titanic’ is not defined

movie = input("What’s your favorite movie? ")

if movie == “Avengers Endgame” or movie == “Titanic”:
print(“Good choice!”)
if movie == “Star Wars”:
print(“Also a good choice!”)

error:
What’s your favorite movie? Star Wars
File “”, line 1
Star Wars
^
SyntaxError: unexpected EOF while parsing

3.A

movie = input("What’s your favorite movie? ")

if movie == “Avengers Endgame” or movie == “Titanic”:
print(“Good choice!”)
elif movie == “Star Wars”:
print(“Also a good choice!”)
else:
print(“You really are an interesting specimen”)

error:
What’s your favorite movie? Star Wars
File “”, line 1
Star Wars
^
SyntaxError: unexpected EOF while parsing

3.B
(Same example cell, same code, different input.)

movie = input("What’s your favorite movie? ")

if movie == “Avengers Endgame” or movie == “Titanic”:
print(“Good choice!”)
elif movie == “Star Wars”:
print(“Also a good choice!”)
else:
print(“You really are an interesting specimen”)

error:
What’s your favorite movie? BeetleJuice

NameErrorTraceback (most recent call last)
in ()
----> 1 movie = input("What’s your favorite movie? ")
2
3 if movie == “Avengers Endgame” or movie == “Titanic”:
4 print(“Good choice!”)
5 elif movie == “Star Wars”:

/etc/jupyter/lib/python2.7/site-packages/ipykernel/ipkernel.pyc in (prompt)
174 self._sys_eval_input = builtin_mod.input
175 builtin_mod.raw_input = self.raw_input
–> 176 builtin_mod.input = lambda prompt=’’: eval(self.raw_input(prompt))
177 self._save_getpass = getpass.getpass
178 getpass.getpass = self.getpass

/etc/jupyter/lib/python2.7/site-packages/ipykernel/ipkernel.pyc in ()

NameError: name ‘BeetleJuice’ is not defined

These errors shows up when I run them in the shell and in the cells inside the kernel. If errors are occurring to the provided untouched code, it must not be my fault. Is this a bug? If not, Is this something I can fix? and if so, how do I fix it?

These example cells that are having errors are all the examples before the exercise. I have yet to attempt the exercise, Although when I do and if it also has errors I will update this post.

Thank you for reading this very long post! And Thank you for any potential help!!!

Hi @wey.tx1,

Thanks for contacting us, sorry for this trouble and welcome to the Community!

This is happening because you have not activated your Python 3 virtualenv. To activate it, run:

source ~/.py3venv/bin/activate

After this, the code you copied from the notebook will work from the terminals.

PS: the code will still not work in the Notebook unless you change input to raw_input or enter your responses in quotes (e.g "Titanic").


Now to the technicalities…
input() works differently in Python 2 and Python 3. Read more here. Until you activate the Python 3 environment, you will be working on Python 2.

1 Like

Hi @wey.tx1,
First of all, welcome to the community! :slight_smile:

I cannot reproduce the:
SyntaxError: unexpected EOF while parsing
Perhaps try resetting the kernel or reloading the course.

As for:
NameError: name ‘Titanic’ is not defined

This error is due to the jupyter notepook (ipykernel) running python2 in this case. In python 2 you have to specify what you are inputting, it is not automatically a string. There are several things you can do:

  1. If you enter 'Titanic' instead of just Titanic it should work.
  2. change input() to raw_input()
  3. execute your script with python3 file.py instead of python file.py (default python is python2 here)

If you look in chapter2, it tells you to activate the python 3 virtual environment by executing this command:
source ~/.py3venv/bin/activate

I assume you forgot this and it is were you problem originates.

@staff Perhaps the interpreter for the jupyter notebook in this tutorial should be changed to python3, as it is a python3 course. And perhaps there should be a reminder at the start of the chapter to launch the virtual environment. Sometimes people may have some days between chapters and forget.

1 Like