@mallquimarco
Try to understand the underlying concepts here. In general, if you want to refer to something later in your code you need to SAVE IT. If you don’t save it, you never have access to that value again. It’s the equivalent of opening a document and writing something very important on it. If you want to see what you wrote earlier and you never saved that document well then you have lost access to that document forever.
Now think about what this code is doing:
input is a function in python. Functions primarily do something and they can return some value or nothing at all and they can also take some values in. So, when you’re calling input("and your name?: ")
you’re passing in the string "and your name?: "
to the input()
function. That works perfectly fine, but the problem here is that input()
also RETURNS a value for you to command.
Now, the problem is that you never save that variable. Because you never save it you have lost it forever; you cannot command anything that you don’t save. So, how do we save variables?
Well we do it using a specific way of speaking to the computer that is:
# I want to command the value 3, but instead of using 3 everytime I need it I can refer to
# it using the name 'x'. The name 'x' isn't special we could call our variable anything!
x=3
# foobar_moobar_i_love_cows is a name that refers to the value 3
foobar_moobar_i_love_cows = 3
#if I wanted to compare values I could use if statements
if foobar_moobar_i_love_cows == 3:
print("Yup that looks good to me! I know foobar_moobar_i_love_cows is 3 because you said so earlier!")
if x:
print("What the hell are you talking about? I don't know what you mean!")
Do you know why the second if statement doesn’t work? Well its because if statements expect the condition i’m testing to be true or false. If I just said x then python has no clue what I’m talking about because x is the value 3 and it isn’t something that is true or false. How would I fix this? Well I need to change the value of x, but how do I do that? I already set it to 3, can I even change it?
x = 3
x = true
if x:
print("oh yes this makes sense!")
This worked because variables can change values over time and the computer will use the value that you assigned to x most recently. So, that’s why this code works but the one before didn’t.
In general, you need to ask yourself, “What are variables?”, “Am I using this function correctly? Did it return a value that I never saved? Are my variables changing how I want them to?”