I am currently working through Automate the Boring Stuff with Python and was given this example in Chapter 4 (You can read the page here if you're curious). The code was typed from the example given in the book as suggested and is pasted below. In the book, I am told the response I get is supposed to prompt me to enter a pet name, and if it doesn't match what's in the list I should get a response saying that I don't have a pet by that name.
The problem I run into is that the response I ACTUALLY get is :
Enter a pet name:
Gennie
Traceback (most recent call last):
File "/Users/gillian/Documents/Python/AutomateTheBoringStuffwithPython/Ch4Example1.py", line 3, in <module>
name = str(input())
File "<string>", line 1, in <module>
NameError: name 'Gennie' is not defined
I'm not sure why that would be. I don't see anything different about my code from the example, but something about that error seems not right. Can anyone tell me where I've gone off course?
myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter a pet name: ')
name = input()
if name not in myPets:
print('I do not have a pet named ' + name)
else:
print(name + ' is my pet.')
Change input() into raw_input() as you seem to be using python 2.x and this code is written in 3.x.
Find out more about differences here.
Related
I'm currently completing an assignment which requires me to create a script to crack a password from a PDF file, I already have a list which contains the password within, I am having issues when prompt to enter the path to the file and met with an Name not define error, please mind I am a novice to coding.
file = raw_input('Path: ')
wordlist = 'wordlist.txt'
word =open(wordlist, 'r')
allpass = word.readlines()
word.close()
for password in allpass:
password = password.strip()
print ("Testing password: "+password)
instance = dispatch('pdf.Application')
try:
instance.Workbooks.Open(file, False, True, None, password)
print ("Password Cracked: "+password)
break
except:
pass
When the program is running, it attempts the first password from the list then proceeds to crash.
python Comsec.py
Path: /home/hall/Desktop/comsec121/examAnswers.pdf
Testing password: 123456
Traceback (most recent call last):
File "Comsec.py", line 11, in <module>
instance = dispatch('pdf.Application')
NameError: name 'dispatch' is not defined
Please excuse my formatting on this website, I am trying my best to help you understand my issue!
Thanks in advance!
This error means that inside your Python script there is no object or function with the name dispatch. You would get that same error if you tried:
instance = this_is_a_function_that_has_not_been_defined('pdf.Application')
Typically, this function should be defined in a Python module. To access it, at the top of your code you should have some import statement like this:
from aBeautifulModuleForPDFs import dispatch
That module will be providing you the missing dispatch function. Checking in Google, I suggest you to try with module pywin32. Install it (run pip install pywin32 in a terminal) and add this line at the beginning of the code:
from win32com.client import Dispatch as dispatch
Edit: Thank you for your helpful answers! I downloaded Python 3.7.0 but you are right, my Mac is running Python 2.7. I have homework now :) Figure out how to get it running 3.7. I will come back if I have more questions. Thank you!
Beginner here. I'm getting NameError when executing in Mac with Python Launcher. When testing in Python 3.7.0 Shell it works ok. I've read other answers to NameError questions, but do not understand what I'm doing wrong. Help is appreciated.
Code used
first_name = input ("Hi, what's your first name? ")
print ("Hi," , first_name)
Error received
Traceback (most recent call last):
File "/Users/imperio/Documents/pythonpractice/Name.py", line 1, in <module>
first_name = input ("Hi, what's your first name? ")
File "<string>", line 1, in <module>
NameError: name 'Imperio' is not defined
This is most likely because you are not executing it using Python 3+.
Please check the output of python -V to see which version you are executing your code with.
On mac you may already have both installed, Python 3 is sometimes aliased under python3 file.py
Here's your program converted to valid Python2:
first_name = raw_input ("Hi, what's your first name? ")
print ("Hi, {}".format(first_name))
You're running Python 2. In Python 2, input executes the input. raw_input just returns the string entered.
Here's an example:
>>> x = 1
>>> y = 2
>>> z = 3
>>> print input('variable? ')
variable? x # Note output is the value of the variable
1
>>> print input('variable? ')
variable? w # Variable 'w' doesn't exist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'w' is not defined
>>> print raw_input('variable? ') # Raw input just returns the input.
variable? x
x
def main():
name = input('Typer your name and press enter: ')
name_list = name.split()
print(name_list)
first = name_list[0][0]
middle = name_list[1][0]
last = name_list[2][0]
print(first.upper(),'.', middle.upper(),'.', last.upper())
main()
I am using python 3.5.2
You are running the code in Python 2, not Python 3... Observe
$ python script.py
Typer your name and press enter: ang go koms
Traceback (most recent call last):
File "script.py", line 13, in <module>
main()
File "script.py", line 2, in main
name = input('Typer your name and press enter: ')
File "<string>", line 1
ang go koms
^
SyntaxError: invalid syntax
Hence, your "error". Lookup the difference in input vs. raw_input... It's a common problem.
Now, try Python3
$ python3 script.py
Typer your name and press enter: ang go koms
['ang', 'go', 'koms']
A . G . K
You can see that my default python is actually Python 2
$ python --version
Python 2.7.13
I can't see any particular issue with the code you put up other than the missing colon. So this is what I have that run successfully. It seems you didn't copy paste the code you are running since you said you have the colon in your code. So maybe try mine and see if there is a difference somewhere character wise.
def main():
name = input('Type your name and press enter: ')
name_list = name.split()
print(name_list)
first = name_list[0][0]
middle = name_list[1][0]
last = name_list[2][0]
print(first.upper(), '.', middle.upper(), '.', last.upper())
main()
You may also want to look at handling when a name is longer or shorter than 3 words.
I'm new to python and have problems with input.
When I'm using command userName = input('What is your name? ')
it says something like this:
>>> userName = input('What is your name? ')
What is your name? Name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Name' is not defined
What I must do with it?
change it to :
userName = raw_input('What is your name?')
In Python 2.x:
raw_input() returns string values and
input() attempts to evaluate the input as command
But in python 3.x, input has been scrapped and the function previously known as raw_input is now input.
The function input() evaluates input as a command--that is why numbers work, but not generic strings that cannot be executed. To achieve attaching a string to a variable, use the more universal raw_input(), which reads everything as a string.
Your new code will now look like
userName = raw_input('What is your name? ')
Best of luck, and happy coding!
This question already has answers here:
"NameError: name '' is not defined" after user input in Python [duplicate]
(3 answers)
Closed 7 years ago.
I am having a spot of bother with the following script, it uses a function within it:
def my_function(arg1, arg2, arg3):
print("I love listening to %s" %(arg1))
print("It is normally played at %d BPM" %(arg2))
print("I also like listening to %s" %(arg3))
print("Dat be da bomb playa!")
print("What music do you like listening to?")
ans1 = input(">>")
print("What speed is that usually played at?")
ans2 = input(">>")
print("whats your second favourite genre of music?")
ans3 = input(">>")
my_function(ans1, ans2, ans3)
I get to the first user input: What music do you like listening to? When I type in 'House' and hit enter, I am expecting the second question to be displayed, but I get the following error message:
Traceback (most recent call last):
File "ex19_MyFunction.py", line 26, in <module> ans1 = input(">>")
File "<string>", line 1, in <module>
NameError: name 'house' is not defined**
Any help would be appreciated!
If you use Python2 then use raw_input() because input() try to parse text as python code and you get error.
Your script would run just fine in Python3, so I am assuming you are using Python2. Change input to raw_input.
In Python2, raw_input will convert the input to a string, while input('foo') is equivalent to eval(raw_input('foo')).