Ask coding questions

← Back to all posts
How do you use the 'if' function in Python?
Intellect103 (3)

How do you use the 'if' function in Python?

I have tried and researched but I was unable to find the right thing to do . I hope anyone can help me. Thanks!!!


Its very easy.

ans = intput("> ")
if ans == "2":
OldWizard209 (1542)

How do you use the 'if' function in Python?

The if function ????. I think you meant if statements. Functions are not the same as statements. The if statement is conditional. It means that if the statement you have provided evaluates to true, it will run the code in its code block. The elif runs only if the first if doesn't work. The else statements handles all exceptions and runs the specified code"

age = 16
if age >= 18:
    print("You are old enough")
elif age < 18:
    print("You are not old enough")
    print("You are nothing")
SirBilby (9)

Here you go:

How to improve the code where an if statement is needed:

print("Answer with  yes  or  no")
sad = input("Do you think it is weird? ")
if sad == "yes"
  print("Oh, thank you.")
elif sad == "no"
  print(name + "that is mean!")