Luca
Luca

Reputation: 11

Extreme beginner having issues with print and def

I bought a course for Python and I am already having issues in the 1st assignment. I am supposed to print something like:

color = ___
thing = ___
print (color + " is the color of " + thing) 

The desired result would be "Yellow is the color of sunshine", but when I just substitute the ___ with the words I want, it tells me I did not "define", the problem is all the previous videos that lead to this assignment never even mentioned def as a function. It never went past printing stuff and doing simple maths exercises with Python. I am a bit lost, please help me out and thank you in advance!

Upvotes: 0

Views: 52

Answers (3)

Dead Guy
Dead Guy

Reputation: 57

You should define color string(like in my code below) and I would suggest using formatted string.

Code:-

color = "XYZ"
thing = "XYZ"

print (f'{color} is color of {thing}')

Upvotes: 0

Luca
Luca

Reputation: 11

Ok nevermind, I believe I have found the answer, I should have wrote the first 2 lines like this:

color = "Yellow"

Without the "" it gave me error but as soon as I included them I got it right. Thank you

Upvotes: 1

orlp
orlp

Reputation: 117771

Yellow is a variable with the name Yellow. "Yellow" (note the quotes) is a string containing the text Yellow.

So color = Yellow tries to assign to color a value from a variable (Yellow) that doesn't exist.

color = "Yellow" does what you want.

Upvotes: 5

Related Questions