Python Forum

Full Version: Getting an error while using input function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
color = input("enter a color: ")
plural_noun = input("Enter a plural noun : ")
celebrity = input("Enter a celebrity: ")

print("roses are {color}")
print("{plural_noun) are blue")
print("i love {celebrity}")
This is the traceback i get

Error:
/Users/dharamchandsethia/PycharmProjects/Volatility/venv/bin/python /Users/dharamchandsethia/PycharmProjects/Volatility/Test.py enter a color: red Traceback (most recent call last): File "/Users/dharamchandsethia/PycharmProjects/Volatility/Test.py", line 1, in <module> color = input("enter a color: ") File "<string>", line 1, in <module> NameError: name 'red' is not defined
Do i need to define the variable color before or i can ask for user input.
Please use tags when posting code.
You are missing a curly bracket on your 2nd print
If your going to use curly you need to put an f (format string) in your print function
print(f"{myvar}")
color = input("enter a color: ")
plural_noun = input("Enter a plural noun : ")
celebrity = input("Enter a celebrity: ")

print("roses are {color}")
print("{plural_noun) are blue")
print("i love {celebrity}")
The curly code is there in print statement and putting the curly code in front of '{' creates error.

Thanks for your reply
you need it to be f0string, e.g.
print(f"roses are {color}")
note the f in front of the string

also, regarding the error in first post - read https://python-forum.io/Thread-Python3-2...-raw-input
you are using python2. use python3. On linux python command is associated with python2, use python3 instead.

finally there is mismatch in brackets:{plural_noun)
color = input("enter a color : ")

print(f"roses are {color}")
tried just with these 2 lines - not working.
(May-11-2020, 04:53 PM)dcsethia Wrote: [ -> ]tried just with these 2 lines - not working
not working isn't helpful. Do you get a traceback? Do you use python3 as already advised? what version of python3 - f-strings were introduced in 3.6....

Python 3.7.7 (default, Mar 10 2020, 17:25:08) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> color = input("enter a color : ")
enter a color : red
>>> print(f"roses are {color}")
roses are red