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.

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?

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.

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)

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:

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")

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")

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?

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?

@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

In the first line current_age is storing the input that the user writes… so I need to store the input about the name…?

1 Like

I am so impressed with how you keep going, Marco! I’m also impressed with the responses from the moderators. They are very helpful in drawing the answer out of you without giving you the answer. Yeah, moderators! Yeah, Marco!

2 Likes

Thanks @camperextraordinaire jeremy @stutterz for your guidance. Sadly it seems that I can reply only every 4 hours or sort of. I am going to review some definitions on my own and try to turn this code around. If not, I´ll be back tomorrow. But I think that functions are involved… If anyone want to send me the last hint of the day please… Thanks for the patience…

It tooke extra time but in the end I did it. I needed extra help, though.

current_age = int(input("Your current age: "))

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

Now I know a little bit more.

Thanks everyone! (I´ll be back)

1 Like

Hallelujah! I hope you learned something from this :smile: !

@mallquimarco The beginning is most important! Really understanding the fundamentals is difficult, but you can master them if you keep trying.

1 Like

Hi , i just started to code , like couple of weeks and was wondering why not use an “else” after the 1st “if”

current_age = int(input("Your current age: "))

if current_age <18:
    print ("Sorry, bye")
else:
    name = input("and your name?: ")
    if name.lower() == "marco":
        print ("Go home")
    else:
        print ("Welcome")

This is what i could come up with , is there anyway this code could break

Hi @keelanurnishanth, have you tried this code ?