FAQ: Control Flow - If Statements

This community-built FAQ covers the “If Statements” exercise from the lesson “Control Flow”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise If Statements

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why are we defining the username outside the scope of the function. Why do i get an error if i dont define the username outside the function

Not sure if the exercise appreciates it, but we can also pass a string directly to the function call:

print(dave_check("Dave"))

the exercise takes an extra step to define a variable first.

Either way, we need an argument for the parameter of the function

Thanks for the explanation

This should not fail the solution

def dave_check(user_name):
    if user_name == "Dave" or user_name == "angela_catlady_87":
        return "Get off my computer Dave!"


# Enter a user name here, make sure to make it a string
user_name = 'scott'

print(dave_check(user_name))

Below is the correct code for the function for the exercise. I thought that after a “return” line in a function, the function “switches off” and lines under the return aren’t read by the program. How come the second conditional statement is read if it is after the return “Get off my computer Dave” line? Is it because the first conditional statement was not met and so the function doesn’t complete the first “return” and so continues reading the code? Any help is much appreciated

def dave_check(user_name):
  if user_name == "Dave":
    return "Get off my computer Dave!"
  if user_name == "angela_catlady_87":
    return "I know it is you Dave! Go away!"

Seems you answer your own question? The first if condition is false, so the return keyword is not reached. Only when a return keyword is reached, is data returned/handed back which signals that the function is done executing

Hey!

Can’t I update the value of user_name by using += so to include other values of the same variable?
For example, can’t I write user_name += "angela_catlady_87" so it includes both Dave and angela_catlady_87?

Thanks!

What exercise is this? Could you please share the exercise url/link?

Sure. Sorry, it was my first post here! Here’s the link

You can’t, because then the string would become "Daveangela_catlady_87"

what you want would require working with lists, something that will taught later. Your way of thinking is certainly the right one :slight_smile: That is a positive you should take from this :slight_smile:

1 Like

By the way, I tried replacing == with the word ‘is’ and it works! Is this okay? I used this in SQL.

depending on the SQL dialect, these comparison operators are different, for example for mysql:

https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html

Yes you are not supposed to do that you cannot update the names of variables with += that’s only for numeric values
your meant to make something new for example
if user_name = “angela_catlady_87”
print(“Get off my computer angela”)

Hi, in some other languages such as Java Script, there are parentheses after “if”, so I tried doing that with python. I noticed adding parentheses around the boolean statement do not change the if statement’s output and doesn’t result in a syntax error. Why’s that? Is there a difference between with parentheses and without? Should I be using parentheses or not for if statements? Thanks.

user_name = "angela_catlady_87" if (user_name == "angela_catlady_87"): print("I know it is you, Dave! Go away!")

vs

user_name = "angela_catlady_87" if user_name == "angela_catlady_87": print("I know it is you, Dave! Go away!")
1 Like

Javascript syntax is different from python, in python the preferred way is without parentheses.

but the parentheses might still be used to change the order of operations:

if (3 + 2) * 6 == 30:
  print(True)
else:
  print(False)

will be true, however if you remove the parentheses, the multiplying (*) takes priority over sum (+).

2 Likes

can someone help me with this; i want to make a user lock,i have made this code but its not working properly : (can someone modify this)

user = print (input ("tell user: "))

if user == "admin":
  print("welcome")

if user != "admin":
  print ("who are you?")  


like when you type admin(in the terminal) this should say ‘welcome’.
when you type anything other than admin(in the terminal) it should say ‘who are you?’
please tell solution

why do you use print on this line:

user = print (input ("tell user: "))

you are to get input, not print/output something

You can also replace your second if condition with an else

2 Likes

I’m not having a question. Just want to say that this section is really funny; its contributor must be an interesting person!

I can’t seem to figure out why this code won’t work.

username = input('What is your username?')
if username == 'Dave':
	print ("I know it's you, Dave!")
if username == 'John':
	print ("Bogger off, John!")

It returns:

Output:

What is your username?Traceback (most recent call last):
  File "script.py", line 1, in <module>
    username = input('What is your username?')
EOFError: EOF when reading a line