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
Related
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.
when i use https://ideone.com
it gave me Runtime error how can i solve this ?
code :
a = int(raw_input())
b = int(raw_input())
c = int(raw_input())
z = a + b + c
print z
This is because on ideone for raw_input() you need to provide the inputs (click stdin or input to open the input entry box) BEFORE you actually run the program by pressing "Run" or "ideone it".
It is not an interactive shell like the python executable.
Sample: https://ideone.com/MvI72f
The actual runtime error being throw is :
Traceback (most recent call last):
File "prog.py", line 1, in <module>
EOFError: EOF when reading a line
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 have a fully working programme that I wish to run. It executes on my friend's laptop, but not mine, (I've saved it to my documents folder) the following is the program:
def DigitCount(n):
#how many decimal digits in integer 'n'
if n<0:
n=-n
digitCount=1
powerOfTen=10
while powerOfTen<=n:
digitCount+=1
powerOfTen*=10
return digitCount
But I keep getting the following error:
>>> DigitCount(100)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
DigitCount(100)
NameError: name 'DigitCount' is not defined
Wait, are you saying you do the following from the command line?
$ python DigitCount.py
$ python
>>> DigitCount(100)
That won't work. You have to do this:
$ python
>>> import DigitCount
>>> DigitCount.DigitCount(100)
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!