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')).
Related
This question already has answers here:
What's the difference between `raw_input()` and `input()` in Python 3? [duplicate]
(6 answers)
Closed 3 years ago.
name = ''
while name != 'your name':
print('Please type your name.')
name = input()
print('Thank you!')
When I input 'your name' or whatever, it doesn't work. I used the Automate the Boring Stuff with Python.
(Error message: Traceback (most recent call last):
File "C:\Users\CSA\Desktop\superami.py", line 4, in <module>
name = input()
File "<string>", line 1, in <module>
NameError: name 'j' is not defined)
This will work in python 2
name = ''
while name != 'your name':
print('Please type your name.')
name = raw_input()
print('Thank you!')
It looks like you're using Python 2, where input will evaluate the string provided by the user.
You should either:
Use Python 3. Python 2 is EOL in 2020.
Use raw_input
I'm trying to make a text game in python and i'm trying to debug the game right now.
I think this code I'm writing is supposed to type out the letters/characters one by one and make a typing effect.
here it is:
def setup_game():
### BACKSTORY TELLING
backstory = "something something boring backstory"
typeout(backstory)
def typeout(x):
time.sleep(0.03)
sys.stdout.write(char)
sys.stdout.flush()
option = input('> ')
if option.lower() == '> ok':
title_screen()
else:
print("please try again\n")
option = input('> ')
#Actual game
def start_game():
print_location()
main_game_loop()
setup_game()
but whatever i do it always gives me an error and i don't know how to fix it.
here it is:
Traceback (most recent call last):
File "textgame.py", line 612, in <module>
setup_game()
File "textgame.py", line 600, in setup_game
typeout(backstory)
File "textgame.py", line 604, in typeout
sys.stdout.write(char)
NameError: name 'char' is not defined
all the lines referenced in the error are in the code from the top.
I did find another post about the:
time.sleep(0.03)
sys.stdout.write(char)
sys.stdout.flush()
part and i tried doing what the answer said but instead it just gave me a different error which is what i have now.
help would be appreciated, thanks
You need to do something like:
sys.stdout.write(x)
Because char is not defined in your code. You're passing x to the function.
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 5 years ago.
Whenever I try to execute this code:
name = input("What's your name?")
print("Hello World", name)
By running the command python myprogram.py on the command line, it gives me this error:
What's your name?John
Traceback (most recent call last):
File "HelloWorld.py", line 1, in <module>
name = input("What's your name?")
File "<string>", line 1, in <module>
NameError: name 'John' is not defined
It asks me the name but as soon as I type it and press enter it crashes, what does the error mean?
Thanks.
In Python 2 you should use raw_input instead of input in this case.
Assuming you are using python 2 when you enter just (John) it interprets that as variable. You will either need to enter ("John"), forcing it to see a string, or use name = raw_input() in the first line.
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.
This question already has answers here:
"NameError: name '' is not defined" after user input in Python [duplicate]
(3 answers)
Closed 8 years ago.
I have this simple python code which throws errors when i run it. I am not sure what is causing this can somebody help shed some light?
Here is my code file test.py:
name = input("What's your name? ")
print("You are great" + name + "!")
age = input("Your age? ")
print("you look great at" + str(age))
and here is the error:
>>>
What's your name? kiran
Traceback (most recent call last):
File "C:\Users\Sunshine\Desktop\test.py", line 1, in <module>
name = input("What's your name? ")
File "<string>", line 1, in <module>
NameError: name 'kiran' is not defined
>>>
if you are using Python 2.7 the correct way of getting a string from user input is using the method raw_input(""), input("") is for python 3.