Name Error-Name not defined-Python 3.6.2 - python-3.x

I'd be very thankful if someone told me why this message keeps coming up. I wanted to create something positive for Christians and got stuck. All the other questions imply that there must be a typo (but I see none) or raw_input for python 2. Is there any OTHER reason this could be happening?
Click here to see Code
The error I'm getting is in line 3 :
Traceback (most recent call last): File
"/Users/AlexTona/Documents/bible code.py", line 3, in
if answer1=="yes": NameError: name 'answer1' is not defined
I've gone through tutorials and many forums but I've decided to try and post the code since its more specific to my needs. Thank you for your help!
answer1:input("Are you unhappy today?-Yes or No")
if answer1=="yes":
print("That's because God loves you!")
elif answer1=="no":
print ("Did you know God loves you?")
else:
print ("ERROR, please answer YES or NO")

If you have retyped the code then you may have introduced other errors. It really is much better to cut and paste. If you retype the code then you type what you think the code should be, rather than what it is, and so you are asking us to find the error in what you think you are doing, rather than in what you are actually doing.
The code you report runs without error.
From the error message you have reported, it looks as if the variable name in line 3 (answer1) is not the same as the variable name in line 1. Check the spelling. Or paste the code back out of SO into the Python interpreter.
And it would really be better to code:
answer1=input("Are you unhappy today?-Yes or No").upper()
Your code prompts the user to enter Yes or No and then complains if the user does either. It asks for Yes and but expects YES. Not friendly.

In the initial image of the code I did this:
answer1:input
instead of this:
answer1=input
Turns out it really was a sintax error.

Related

KD_raj185 rpgle code in as400 try this please

an assignment operator is expected with the eval operation
a semi colon is not specified at the end of a free format statement
the name or indicator is not defined
the compiler cannot determine how the program can end try this please
hope its clear for issue
#1 & 2 are pretty evident, the error tells the line number in the source with the problem, just fix it. #3 is pretty evident as well, the error will tell you the name of the variable that is not defined.
#4 is the only one that is a bit tricky if you haven't seen it before. The compiler did not find a return from the main procedure, and also no place that *INLR is set on. Either of those things will fix that one.
Odds are that you just included some typos in your code. Please don't paste the entire source and error listing, just read the errors, and go find someone in your company that can tell you where in the source listing to find the errors. Or if you are using RDi, you could just click on the error, and it will take you directly to the problem.

python keeps saying that 'imput is undefined. how do I fix this?

Please help me with this. I'd really appreciate it. I have tried alot of things but nothing is working, Please suggest any ideas you have.
This is what it keeps saying:
name = imput('hello')
NameError: name 'imput' is not defined
You misspelled input as imput. imput() is not a function that python recognizes - thus, it assumes it's the name of some variable, searches for wherever that variable was declared, and finds nothing. So it says "this is undefined" and raises an error.
imput isn't a built-in function. Perhaps you're looking for input.

Python3 - "ValueError: not enough values to unpack (expected 3, got 1)"

I'm very new to Python and programming overall, so if I seem to struggle to understand you, please bear with me.
I'm reading "Learn Python 3 the Hard Way", and I'm having trouble with exercise 23.
I copied the code to my text editor and ended up with this:
import sys
script, input_encoding, error = sys.argv
def main(language_file, encoding, errors):
line = language_file.readline()
if line:
print_line(line, encoding, errors)
return main(language_file, encoding, errors)
def print_line(line, encoding, errors):
next_lang = line.strip()
raw_bytes = next_lang.encode(encoding, errors=errors)
cooked_string = raw_bytes.decode(encoding, errors=errors)
print(raw_bytes, "<====>", cooked_string)
languages = open("languages.txt", encoding = "utf-8")
main(languages, input_encoding, error)
When I tried to run it I got the following error message:
Traceback (most recent call last):
File "pag78.py", line 3, in <module>
script, input_encoding, error = sys.argv
ValueError: not enough values to unpack (expected 3, got 1)
which I am having difficulties understanding in this context.
I googled the exercise, to compare it something other than the book page and, if I'm not missing something, I copied it correctly. For example, see this code here for the same exercise.
Obviously something is wrong with this code, and I'm not capable to identify what it is.
Any help would be greatly appreciated.
When you run the program, you have to enter your arguments into the command line. So run the program like this:
python ex23.py utf-8 strict
Copy and paste all of that into your terminal to run the code. This exercise uses argv like others do. It says this in the chapter, just a little bit later. I think you jumped the gun on running the code before you got to the explanation.
Let's record this in an answer for sake of posterity. In short, the immediate problem described lies not as much in the script itself, but rather in how it's being called. No positional argument was given, but two were expected to be assigned to input_encoding and error.
This line:
script, input_encoding, error = sys.argv
Takes (list) of arguments passed to the script. (sys.argv) and unpacks it, that is asigns its items' values to the variables on the left. This assumes number of variables to unpack to corresponds to items count in the list on the right.
sys.argv contains name of the script called and additional arguments passed to it one item each.
This construct is actually very simple way to ensure correct number of expected arguments is provided, even though as such the resulting error is perhaps not the most obvious.
Later on, you certainly should check out argparse for handling of passed arguments. It is comfortable and quite powerful.
I started reading LPTHW a couple of weeks ago. I got the same error as 'micaldras'. The error arises because you have probably clicked the file-link and opened an IEExplorer window. From there, (I guess), you have copied the text into a notepad file and saved. it.
I did that as well and got the same errors. I then downloaded the file directly from the indicated link (right click on the file and choose Save Target As). The saves the file literally as Zed intended and the program now runs.

python error-can only join an iterable

I am making a program in pygame and I am trying to create with buttons where the user can enter their name. So basically when ever they click on the letter, then it puts that letter into a list. I recreated with about the same variables as in my game and got the same error. So if we can fix this, then I can fix my game. I am using functions so that is why you see the global thing as that is needed. Here is the code. I have looked at other forums btw but nothing that is the same as mine.
import glob
unknown=[]
def bob():
global unknown
a=('a').upper()
unknown=unknown.extend(a)
unknown=','.join(unknown)
bob()
Again, this is basically what is in my code except for the name of the function and the name of the list, other than that it is the same. The name doesn't matter though. Thanks in advance.

Python3.4 Anaconda: Input() Function Broken?

I'm having trouble with the input() function in Python3.4 using the Anaconda integrated editor. If I just type
x = input()
into the editor, it returns a blank line that I can type text into. If I type:
foo
into this line, I would expect 'foo' be stored as a string with variable name x. But, instead I get:
NameError: name 'foo' is not defined
To make the function work as expected, I must instead type in:
'foo'
which is unfortunate because what I really want is just to pause my code and wait for an arbitrary user input, and I read somewhere that "wait = input()" is the most pythonic way to do this. Using that line in my actual script returns an "unexpected EOF" error - I assume as another symptom of the same problem. Can anyone suggest a workaround?
Note: I suspect this is an Anaconda-specific problem, given the following reference:
https://docs.python.org/3.4/library/functions.html#input
Thanks for your time.
Your code is being run by Python 2, not 3. I don't know enough about Anaconda to know if the problem is with their editor, or if you have your path messed up, but the problem is that the wrong version of Python is being used.

Resources