input string in python 3
input string in python 3
I have string variable test, In Python 2.7 this works fine.
test = raw_input("enter the test")
print test
But in Python 3.3. I use
test = input("enter the test")
print test
with the input string sdas, and I get an error message
sdas
Traceback (most recent call last):
File "/home/ananiev/PycharmProjects/PigLatin/main.py",
line 5, in
test = input("enter the test")
File "", line 1, in NameError: name 'sdas' is not defined
7 Answers
7
You're running your Python 3 code with a Python 2 interpreter. If you weren't, your print statement would throw up a SyntaxError before it ever prompted you for input.
print
SyntaxError
The result is that you're using Python 2's input, which tries to eval your input (presumably sdas), finds that it's invalid Python, and dies.
input
eval
sdas
I'd say the code you need is:
test = input("enter the test")
print(test)
Otherwise it shouldn't run at all, due to a syntax error. The print function requires brackets in python 3. I cannot reproduce your error, though. Are you sure it's those lines causing that error?
print
sdas is being read as a variable. To input a string you need " "
Are you sure? As far as I did understand, the OP wants zu pass a defined variable so the script does evaluate the variable.
– reporter
Sep 26 '14 at 10:55
Can you elaborate what you wants to say? It would be better if you provide code snippet?
– rajesh ujade
Sep 26 '14 at 11:02
I got the same error. In the terminal when I typed "python filename.py", with this command, python2 was tring to run python3 code, because the is written python3. It runs correctly when I type "python3 filename.py" in the terminal. I hope this works for you too.
Or use the shebang in the python script :). More info, for example, here: stackoverflow.com/questions/6908143/…
– s3n0
Mar 3 at 15:06
In operating systems like Ubuntu python comes preinstalled. So the default version is python 2.7 you can confirm the version by typing below command in your terminal
python -V
if you installed it but didn't set default version you will see
python 2.7
in terminal. I will tell you how to set the default python version in Ubuntu.
A simple safe way would be to use an alias. Place this into ~/.bashrc or ~/.bash_aliases file:
alias python=python3
After adding the above in the file, run the command below:
source ~/.bash_aliases or source ~/.bashrc
source ~/.bash_aliases
source ~/.bashrc
now check python version again using python -V
python -V
if python version 3.x.x one, then the error is in your syntax like using print with parenthesis. change it to
test = input("enter the test")
print(test)
Well question is very simple, i don't know why people have answer this badly.
Solution:
If using python 2.x :
then for evaluated input use "input"
example: number = input("enter a number")
and for string use "raw_input"
example: name = raw_input("enter your name")
If using python 3.x :
then for evaluated result use "eval" and "input"
example: number = eval(input("enter a number"))
for string use "input"
example: name = input("enter your name")
temperature = input("What's the current temperature in your city? (please use the format ??C or ???F) >>> ")
### warning... the result from input will <str> on Python 3.x only
### in the case of Python 2.x, the result from input is the variable type <int>
### for the <str> type as the result for Python 2.x it's neccessary to use the another: raw_input()
temp_int = int(temperature[:-1]) # 25 <int> (as example)
temp_str = temperature[-1:] # "C" <str> (as example)
if temp_str.lower() == 'c':
print("Your temperature in Fahrenheit is: ".format( (9/5 * temp_int) + 32 ) )
elif temp_str.lower() == 'f':
print("Your temperature in Celsius is: ".format( ((5/9) * (temp_int - 32)) ) )
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
Python 3 input function is well explained in this video: youtube.com/watch?v=i7Y9ugHJJUg.
– Ishaq Khan
Mar 9 at 3:49