this is my first post on this site and please tell me if I posted on wrong place or something.
So... I'm using Mac version of Python 3.x which I started learning a few weeks ago and am facing a bit of trouble understanding here.
In the text editor, I wrote and saved:
>a = input("> ") <br>
print("A boy goes to" + a)
And then:
> >
But returned me with:
> > school
Traceback (most recent call last):
File "workspace/main.py", line 3, in <module>
a = input("> ")
File "<string>", line 1, in <module>
NameError: name 'school' is not defined
What did I do wrong?
It's a bit unclear what you've done, and those "> >" are a bit odd to me, usually python has 3 ">" when you execute it.
The input function in python stops the execution and waits until the user types something (or nothing) and presses the return key (enter). You can assign whatever the user inputed from his keyboard into a variable, as you did.
variable = input("Some text to show the user what he should do")
# Execution will stop until user presses enter
print(variable) # Will print whatever the user typed when the above text was printed to him.
One thing to notice is: if you execute python in interactive mode, it will ask for you to enter your input right after you ask for the user's input value.
If you are using python 2.7 write school in double quotes to get it as string.
E.g. an example from Python 2.7 idle:
>>> a = input("> ")
> "school"
>>> print("A boy goes to " + a)
A boy goes to school
Related
I'm a college student who is fairly new to both Python and VSC and am experimenting with creating my own MadLibs program. I have my main file that asks the user which story they would like, and depending on their answer, imports the correct story.
Here is my main code:
#Mad Libs
#5/4/2022
num = int(input('Input a number 1-9 to choose a story!'))
if num == 1:
import MadLibs1
MadLibs1.story1
And here is my MadLibs1 code (shortened):
def story1():
verb1 = str(input('Enter an ing-verb: '))
place1 = str(input('Enter a physical location: '))
holiday_song = str(input('Enter a holiday song: '))
story = f'HOLIDAY MADNESS \n \nI was {verb1} at the {place1} the other day \
when I heard {holiday_song} come on the radio.\n\
print(story)
print('')
MadLibs1 is another python file I have, and story1 is a function that takes nouns, verbs, etc. and prints the revised mad libs story.
My problem: when I run the program, it correctly asks me to input a number. When I input the number and press Enter, the following shows up:
PS C:\Users\joshu\OneDrive - University of Missouri\Documents\Python files VSC\Mad Libs>
And it expects me to provide an input. I'm not sure why it's wanting another input. It's just supposed to go straight to executing the function that is in MadLibs1.
Anyone have any idea what I am doing wrong?
Thanks!
Josh
To import a file you need to import with the file extension. For example...
import filename.py
EDIT
In the MadLibs file you need to actually call the function. All you have done is declare the function. You call a function by typing the function name with parentheses and any required arguments. This needs to be done underneath the declared function like this
story ()
I'm a complete newbie when it comes to coding, so I'm probably making a stupid mistake here. What I'm doing is following along with a Python For Beginners textbook and writing the 'Hello World' exercise. I added the variable 'name' to the code and called the variable in the very next line. However, the
print('Hello', name) line of code I wrote would not print, even though my first line print('Hello, world') did print. Here's a screenshot of the code run on the Python console There are no error codes, so I'm guessing I just incorrectly defined the variable, but I'm not sure.
You need to hit Enter after Python prints 'Keenan' to print the last line.
When you call input(str), str is the prompt. Once python prints the prompt, it's waiting for an input and then Enter.
So logically speaking your code should be:
name = input("What's your name?:")
print('Hello', name)
Which will print:
What's your name?:
where you can type:
What's your name?: Keenan
followed by Enter
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 7 years ago.
I'm completely lost as to why this isn't working. Should work precisely, right?
UserName = input("Please enter your name: ")
print ("Hello Mr. " + UserName)
raw_input("<Press Enter to quit.>")
I get this exception:
Traceback (most recent call last):
File "Test1.py", line 1, in <module>
UserName = input("Please enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'k' is not defined
It says NameError 'k', because I wrote 'k' as the input during my tests. I've read that the print statement used to be without parenthesis but that has been deprecated right?
Do not use input() in 2.x. Use raw_input() instead. Always.
In Python 2.x, input() "evaluates" what is typed in. (see help(input)). Therefore, when you key in k, input() tries to find what k is. Because it is not defined, it raises the NameError exception.
Use raw_input() in Python 2.x. In 3.0x, input() is fixed.
If you really want to use input() (and this is really not advisable), then quote your k variable as follows:
>>> UserName = input("Please enter your name: ")
Please enter your name: "k"
>>> print UserName
k
The accepted answer provides the correct solution and #ghostdog74 gives the reason for the exception. I figured it may be helpful to see, step by step, why this raises a NameError (and not something else, like ValueError):
As per Python 2.7 documentation, input() evaluates what you enter, so essentially your program becomes this:
username = input('...')
# => translates to
username = eval(raw_input('...'))
Let's assume input is bob, then this becomes:
username = eval('bob')
Since eval() executes 'bob' as if it were a Python expression, your program becomes this:
username = bob
=> NameError
print ("Hello Mr. " + username)
You could make it work my entering "bob" (with the quotes), because then the program is valid:
username = "bob"
print ("Hello Mr. " + username)
=> Hello Mr. bob
You can try it out by going through each step in the Python REPL yourself. Note that the exception is raised on the first line already, not within the print statement.
This question already has answers here:
input() error - NameError: name '...' is not defined
(15 answers)
Closed 7 years ago.
I'm completely lost as to why this isn't working. Should work precisely, right?
UserName = input("Please enter your name: ")
print ("Hello Mr. " + UserName)
raw_input("<Press Enter to quit.>")
I get this exception:
Traceback (most recent call last):
File "Test1.py", line 1, in <module>
UserName = input("Please enter your name: ")
File "<string>", line 1, in <module>
NameError: name 'k' is not defined
It says NameError 'k', because I wrote 'k' as the input during my tests. I've read that the print statement used to be without parenthesis but that has been deprecated right?
Do not use input() in 2.x. Use raw_input() instead. Always.
In Python 2.x, input() "evaluates" what is typed in. (see help(input)). Therefore, when you key in k, input() tries to find what k is. Because it is not defined, it raises the NameError exception.
Use raw_input() in Python 2.x. In 3.0x, input() is fixed.
If you really want to use input() (and this is really not advisable), then quote your k variable as follows:
>>> UserName = input("Please enter your name: ")
Please enter your name: "k"
>>> print UserName
k
The accepted answer provides the correct solution and #ghostdog74 gives the reason for the exception. I figured it may be helpful to see, step by step, why this raises a NameError (and not something else, like ValueError):
As per Python 2.7 documentation, input() evaluates what you enter, so essentially your program becomes this:
username = input('...')
# => translates to
username = eval(raw_input('...'))
Let's assume input is bob, then this becomes:
username = eval('bob')
Since eval() executes 'bob' as if it were a Python expression, your program becomes this:
username = bob
=> NameError
print ("Hello Mr. " + username)
You could make it work my entering "bob" (with the quotes), because then the program is valid:
username = "bob"
print ("Hello Mr. " + username)
=> Hello Mr. bob
You can try it out by going through each step in the Python REPL yourself. Note that the exception is raised on the first line already, not within the print statement.
I tried to write my first python program and I already get an error message. In the textbook introduction to computer science using python i found the following code:
name = input('What is your name? ')
print('Hello', name)
print('Welcome to Python!')
I checked multiple times for errors and I'm quite sure i typed it exactly like the textbook states. I saved the program as MyFirstProgram.py and after that i ran the module (by pressing F5). If i understand correctly the program asks you to fill in a name. So i typed 'John'. But when i did, the following error occurs:
Traceback (most recent call last):
File "C:/Users/Wout/.ipython/MyFirstProgram.py", line 3, in <module>
name = input('What is your name? ')
File "<string>", line 1, in <module>
NameError: name 'John' is not defined
Why is 'John' not defined? Isn't it the purpose of the program to enter any name? Why do i have to define it? I followed the instructions to the letter...
Kind regards
input, in Python 2, evaluates the input as if it were a snippet of Python code. This is almost never what you want. Use raw_input instead.
By the way, you're writing your code as if it were Python 3, but you appear to be using a Python 2 interpreter. If you run your code with Python 3, it will work fine (input in Python 3 is the same as raw_input in Python 2).
You should use raw_input() instead of an input(), since you are on python-2.x:
name = raw_input('What is your name? ')
print('Hello', name)
print('Welcome to Python!')
prints:
What is your name? John
('Hello', 'John')
Welcome to Python!
You are following a textbook for Python 3 but using Python 2. In Python 2, must use raw_input and don't need brackets on print statements.
'John' will work with input (John won't work), however you should use raw_input() like the others said