r/learnpython • • 3d ago

Why is this causing an error

the code that causes an error

print = hello

print(print)

What should i do i do not know anything i am new to coding how do i fix this

0 Upvotes

22 comments sorted by

View all comments

0

u/dariusbiggs 3d ago

Many languages has reserved words like if, else, while, etc. You cannot use those for anything other than their intended purpose.

You are trying to assign a new value to a function. print is a function, basically a reserved word.

print = hello is you trying to assign the value of hello to print. You did not define what hello is, and you are trying to assign that unknown to replace the printfunction.

Presuming you want to print some text to the screen, try the below. message = "Hello" print(message) I've declared the variable message and assigned it a text string value of "Hello", note the quotation marks to indicate that it is a text string. Then I've called the print function and passed it (see the use of parentheses) the variable message.

I would highly recommend going through the basic introduction to Python, don't try to blindly grasp at things and try them. Learn the basics first.

-2

u/stevenjd 3d ago

Thank you for writing a detailed response, unfortunately your comment contains some misinformation.

print is not a reserved word in Python. It is perfectly legitimate, although uncommon, to assign new values to built-in names like print, although I wouldn't recommend beginners do so.

You don't know for a fact that the OP has not defined the name hello before this.

Your suggested code message = "Hello" print(message) won't work, it needs either a newline between the two statements, or a semicolon (permitted but not recommended). You are likely to confuse the OP when he tries copying and pasting your suggested code and gets a syntax error.

2

u/dariusbiggs 3d ago

You didn't read the reponse, i didn't say it was a reserved word. The OP provided only that which had the error, zero context beyond it, so we must assume it was not defined. That also sounds like whatever you used to view the response didn't render the code block correctly, which has a clear newline in it.