Ok, so I'm writing a grade checking code in python and my code is:
unit3Done = str(input("Have you done your Unit 3 Controlled Assessment? (Type y or n): ")).lower()
if unit3Done == "y":
pass
elif unit3Done == "n":
print "Sorry. You must have done at least one unit to calculate what you need for an A*"
else:
print "Sorry. That's not a valid answer."
When I run it through my python compiler and I choose "n"
, I get an error saying:
"NameError: name 'n' is not defined"
and when I choose "y"
I get another NameError
with 'y'
being the problem, but when I do something else, the code runs as normal.
Any help is greatly appreciated,
Thank you.
You are using the
input()
function on Python 2. Useraw_input()
instead, or switch to Python 3.input()
runseval()
on the input given, so enteringn
is interpreted as python code, looking for then
variable. You could work around that by entering'n'
(so with quotes) but that is hardly a solution.In Python 3,
raw_input()
has been renamed toinput()
, replacing the version from Python 2 altogether. If your materials (book, course notes, etc.) useinput()
in a manner that expectsn
to work, you probably need to switch to using Python 3 instead.Use
raw_input
in Python 2 to get a string,input
in Python 2 is equivalent toeval(raw_input)
.So, When you enter something like
n
ininput
it thinks that you're looking for a variable namedn
:raw_input
works fine:help on
raw_input
:help on
input
: