Python Forum
Getting an error while using input function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting an error while using input function
#1
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.
Reply
#2
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}")
Reply
#3
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
Reply
#4
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
color = input("enter a color : ")

print(f"roses are {color}")
tried just with these 2 lines - not working.
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Function with array input Gerkamspiano 5 195 Dec-01-2020, 08:48 AM
Last Post: Gerkamspiano
  Pyspark SQL Error - mismatched input 'FROM' expecting <EOF> Ariean 3 422 Nov-20-2020, 03:49 PM
Last Post: Ariean
  calling a function and argument in an input phillup7 3 280 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  EOF error while taking input ShishirModi 1 325 Sep-27-2020, 11:28 AM
Last Post: jefsummers
  Input Error Dream 2 512 Jul-12-2020, 05:41 PM
Last Post: bowlofred
  Use of input function to change screen background color in Turtles Oldman45 3 655 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  Input() function not working in VS Code darpInd 7 960 Feb-17-2020, 03:28 PM
Last Post: snippsat
  Using input function in a Class wew044 2 541 Feb-06-2020, 03:08 AM
Last Post: wew044
  what would you call the input for the parameter(s) of a function you have defined? rix 3 600 Dec-16-2019, 12:04 AM
Last Post: rix
  Mathematical function input Tiiill 2 605 Sep-10-2019, 09:25 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020