Another beginner query

I continue playing with my first codes. I can not solve this one. I want to discriminate all those who are under 18 years old for sure. If someone is 18 or over 18 I want to ask them their name. And if anyone of them is named Marco I want to discriminate him to, the rest are welcomed. So far I have this:

current_age = int(input("Your current age: "))
current_name = input("and your name? :")

if current_age < 18:
    print ("Sorry, you are out")
else:
    print (current_name)
    if current_name == "Marco":
        print ("go home")
    else:
        print ("welcome")

I am breaking my brain for one hour and I can not figure it out.

Thanks in advance.

If you only want to ask the name if the user is 18 or over, then you should not be getting the input before asking about the age.

Thanks, with your help I have reached this code:

current_age = int(input("Your current age: "))
current_name = ""
if current_age < 18:
    print ("Sorry, you are out")
elif current_age >= 18:
    print (input("and your name?: "))
elif current_name != "Marco":
    print ("Welcome")
else:
    print ("Go home")

With this I have solved the first part, all those under 18 are discriminated. If the age is 18 or more python ask the name. But when I write the name Marco, It answer Marco again. And if I write another name, it answer that name as well.
I know I am missing something that is front of me, but I can not realize what. Do you have another hint?

If the user is 18 or older and the current name is Marco, what do you want to happen?

If the user is 18 or older and the current name is not Marco, what you want to happen?

I ask these questions, because currently, the following line will always print the name entered by the user who is 18 or older.

    print (input("and your name?: "))

You have added elif and and else statement that will never execute, because if the current age is less than 18, one thing happens and if not, the first elif statement’s result will print whatever name is entered. The other two conditions will not be considered.

Ok, I forgot to tell you that it is required to ask the name of the user when, and only, if the age is 18 or over 18. That´s why I wrote that line:

print (input("and your name?: "))

So, when the user is 15 (for instance) the answer is “Sorry, you are out”, which is ok.
When the user is 19 (for instance) the answer is “and your name?” which is ok.
When the user types Jack, the answer is Jack, which is not ok (should be welcome)
When the user types Marco, the answer is Marco, which is not ok (should be Go home).

I have tried with functions but it is worst.

So based on your current description, you do not really want to ever display the user’s name again after the user enters it. Is that correct? If so, then, you should not be using the print command on the input as you are in that line. Instead, you want to assign the input enter to another variable that can be used in completely separate if/else statements. Currently, you have one set of if/elif/else statements instead of two unique sets of statements.

Ok, you are right, I understood that part. So, so far I have these lines:

current_age = int(input("Your current age: "))
current_name = "Marco"
not_allowed = "Go home"
allowed = "Welcome"
if current_age <18:
    print ("Sorry, bye")
if current_age >= 18: 
    input ("and your name?: ")
    if current_name == "Marco" and current_age >= 18 :
        print (not_allowed)
    else:
        print (allowed)

and I get these results:

  1. If the user is 17 y.o (expected result :slight_smile:)
Your current age: 17
Sorry, bye
  1. If user is 27 y.o and named Marco (expected result :slight_smile:)
Your current age: 27
and your name?: marco
Go home
  1. If the user is 29 y.o and called Lola (not expected result :frowning: should be “Welcome”)
Your current age: 29
and your name?: Lola
Go home

As you can see, the last part is the problem. This time I have used nested if. I am close, I can feel it. Another hint please.
Thanks in advance and sorry for my grammar (english in process, as well haha)

current_name will always be “Marco” because you assigned the value to it only line 2 of your code and never change it. You need to think about where you should assign a value to current_name and what value should be assigned.

The and current_age >= 18 is not needed because you already know current_age is greater or equal to 18 based on the 2nd if statement.

1 Like

Ok, closer (and shorter).

current_age = int(input("Your current age: "))
if current_age <18:
    print ("Sorry, bye")
if current_age >= 18: 
    input ("and your name?: ")
    if "Marco":
        print ("Go home")
    else:
        print ("Welcome")

The problem with “welcome” remains. I can not show it when the name is not Marco. “Go home” persist in the answer.

Your current age: 33
and your name?: Lola
Go home

I think is the last hint :face_with_raised_eyebrow:

The above two lines are the problem. You ask the user for name, but do not store it. In the second line you are asking if “Marco”. If Marco what? You should be comparing “Marco” to something.

1 Like

Close… to put this in words, if [something goes here] "Marco". What is the missing [something]?

Ok, I have created the variable “name” to store the name. The part that I do not understand is about “comparing Marco to something”

current_age = int(input("Your current age: "))
name = ""
if current_age <18:
    print ("Sorry, bye")
if current_age >= 18: 
    input ("and your name?: ")
    if name == "Marco":
        print ("Go home")
    else:
        print ("Welcome")

Based on your new code, name will always be an empty string. Go back and read my previous reply. I explain the issue with your input.

I have added “==” but the issue remains. This simple code is driving me crazy.

current_age = int(input("Your current age: "))
name = ""
if current_age <18:
    print ("Sorry, bye")
if current_age >= 18: 
    input ("and your name?: ")
    if name == "Marco":
        print ("Go home")
    else:
        print ("Welcome")

This is the correct comparison, but since name never changes after you first assign it an empty string on line 2, even if the user enters “Marco”, it will never display “Go home”.

ok, so…

current_age = int(input("Your current age: "))
name = "Marco"
if current_age <18:
    print ("Sorry, bye")
if current_age >= 18: 
    input ("and your name?: ")
    if name == "Marco":
        print ("Go home")
    else:
        print ("Welcome")

to store “Marco” as the only value, right?

Why would you only want to store “Marco” as the only name? This is not necessary. You only care about the name, if the current age enter is greater or equal to 18 and then you want to “get” the name and store it, so that when you reference it in the following if statement, it will do as you want.

    if name == "Marco":

You are currently getting a name, but you are not able to reference it, because you are not assigning it to anything.

so, with:

name = ""

I am storing the name that the user writes, to pyhton make with it whatever I command. Now, what I do not get is how to assign a value to that name. Should I assign that value in the first lines?

No, you are not. That line just stores a blank string as name.

Look the first line of your code. What does that line do?

@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?”

1 Like