0
What is the difference between the value and the variable name if the variable message and value Game over print("Game over")
Variable print
8 Respostas
+ 5
print() is a function, not a variable
+ 3
in your example "message" is a string as can be seen from the "". there is no variable called message
+ 3
Ugulberto Sánchez you have confused double-quotation marks (") with commas (,).
Asep Sujana strings are denoted by single- or double-quotation marks (not commas).
+ 3
What is the difference between the value and the variable name if the variable message and value Game over print("Game over") instead of print("message")
message = “Game Over”
print(message)
#Prints the context of the variable.
print(“Game Over”)
#Prints the string.
In this example it does not make much of a difference since the result is the same. But if you have to write a more sophisticated code then using a variable would be more beneficial.
Example:
player_score = 1000
message = False
if player_score >= 1000:
message = True
if message:
print("Game Over!")
else:
print("Continue playing!")
It would make more sense to use game_over as variable instead of message. But for demonstration I have used message as variable, you may check if message is true or false with print(message)
+ 1
Asep Sujana,
"string" datatype is surrounded by double question marks and it is interpreted as text
for example:
"hello!"
to call a variable, use its name WITHOUT double question marks
for example:
my_var = 34
print("my_var") -> prints my,_var in the console
print(my_var) -> prints 34
Doy you understand?
+ 1
#Variable
msg = "hi"
print(msg)
#No variable
print("hi")
These two provide the same result, but it is not always the same.
- We use the first one to get the result of performing stuff or complex things. It can have multiple different results, instead of knowing the answers already and write it down(This is useless when interacting with user input)
- We use the second one, to be sure that it should appear everytime executing the code.
Example:
name = input()
print('Hi",name) //This would show "Hi" everytime executing, but the "name" variable can be different strings each time running, because it is used to get user input.
another example(for loop):
n = int(input())
for x in range(0,n):
print("Hello",x)
0
What is the difference between the value and the variable name if the variable message and value Game over print("Game over") instead of print("message")
0
Brian changed 👍