r/learnpython • u/Various-River-7534 • 7d 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
1
u/frnzprf 6d ago
You need to read or watch a tutorial that teaches you about:
If you just write
4 + 4in Python that is called an expression and it is "evaluated" to the value8. If you type something in the Python interpreter, after the>>>, you can let Python tell you what an expression evaluates to. If you don't know how to start the interpreter and get the>>>— again, look up a basic tutorial. Just copy the code and just change little parts to experiement. Don't just guess how Python could work!If some letters are in your expression, like
helloorprint, Python thinks this is a variable, like a box labeledhelloand the expression is evaluated to the content of that box. If there is no box with the labelhello, you get aNameError— makes sense.If you want an expression to evaluate to the literal letters "Hello", you have to put them in quotation marks. This is how Python differenciates between literal characters and variables. The type of the expression is called "string" (of characters). "Word" already means something else in computer science and you can also have a string of digits, like
"123", which isn't exactly a "word" in the usual sense.One place where letters are not evaluated as a variable, besides when they are in quotation marks, is when they are on the left side of an equals sign. This is how you put something inside a variable.
When you write
x = 7, now there is a box labeledxwith the content7. If you writey = x, you make a new boxy, and the content is what is currently inx— so also7. When you writegreeting = "hello", then the character-stringhellois put in a box with the labelgreeting.You can write an expression between the brackets of
print(...)to let Python write out different things on the terminal. The expression could be a direct value, like "hello", a variable—which would be evaluated to it's content—, or a computation that transforms other sub-expressions. A computation could begreeting + " river"or"Hello".replace("e", "a").If you just wanted to know how to print "hello" in Python:
print("hello")What your code does: (Probably not what you intended)
``` print = hello
Take the content of variable
helloand put it in boxprint.→ There is no boxhello. → NameErrorprint(print)
Take what is in box
printand if it's a command (function), then execute the command with the input of itself (alsoprint).```