Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
input
#1
I am in the process of learning Python, very much a beginner, and trying to make a simple quiz. However, this simple code does not work, and I cannot figure out what is wrong. I am using PyCharm community edition IDLE.

 answer = input("Answer Here: ")

if answer == "a":
    print("a")

elif answer == "An":
    print("An")

else:
    print("incorrect")
I input a into the input and get this:

/home/steven/PycharmProjects/practice/venv/bin/python /home/steven/PycharmProjects/practice/practicefile
Answer Here: a
Error:
Traceback (most recent call last): File "/home/steven/PycharmProjects/practice/practicefile", line 1, in <module> answer = input("Answer Here: ") File "<string>", line 1, in <module> NameError: name 'a' is not defined
Process finished with exit code 1

Any advice greatly appreciated.
Reply
#2
this is python3 code and you are running it with python3 python 2
you better start using python3. if, for any reason outside your control, you have to use python 2, instead of input use raw_input
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
While working with PyCharm or ipython you have to give the program a string, else it is searching for a variable.
When you execute it on your console by using python3 practicefile.py you can input anything, it will be interpreted as string.
So using PyCharm or ipython simply give the Program "This is a random string" instead of This is a random string:
Or use raw_input instead of input :)
Your code is correct
Reply
#4
(May-02-2018, 03:24 PM)Steven4567 Wrote: PyCharm community edition

Somewhere in the options, there should be a place to pick which version of python it's using. You should pick 3, as you're writing python 3 code, but running it with the old interpreter.
Reply
#5
The option options @nilano talk about is Configuring Python Interpreter.
Here you can view list of available interpreters and choose local(your OS Python Interpreter) or virtual environment(venv) Python Interpreter.
For Linux look at Linux Python 3 environment you should run at least 3.5 preferably 3.6.
Reply


Forum Jump:

User Panel Messages

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