Python Python Basics Types and Branching Use an if

Python Basics

Can somebody please explain what I did wrong? I am new and trying to get Python to finally click in my head.

The code to accept input for the favorite color has been provided for you. For this task, add a branch that checks if the favorite_color is equal to "blue". If it is indeed "blue" print out to the screen, "You must love the sky!"

branching.py
favorite_color = input("What is your favorite color?  ")
print("Blue", "favorite_color")
if favorite_color == "blue":
print("you must love the sky!")

8 Answers

Hi I hafavorite_color = input("What is your favorite color? ") if favorite_color == "blue": print("you must love the sky!")d the same mistake.How I solved it is to make sure the line 3 is pushed forward..

rnw98613
rnw98613
347 Points

Take care of indentation, it is strict about indentation in python, because they replaces the curly braces

favorite_color = input("What is your favorite color?  ")

if favorite_color == "blue":
  print("you must love the sky!")

Unlike in other languages, indentation of nested blocks in Python is required. Indentation in other languages is only "style", the set of standard conventions programmers use to make code easier to read and understand.

Brandon Oakes
Brandon Oakes
Python Web Development Techdegree Student 11,499 Points

You are very close my friend. First off, your code on line 2 is not needed.

   print("Blue", "favorite_color")

so after you git rid of that all you need to do is indent your print statement after your if statement shown below.

  favorite_color = input("What is your favorite color?  ")
  if favorite_color == "blue":
      print("you must love the sky!")

When using an if statement, you are an essence testing a condition. So here you are saying if the value for favorite_color is equal to (==) blue than perform . the desired code. You need to always make sure to indent the code you want to run under the conditional statement to make sure the code ONLY runs if the condition is true. Let me know if that makes sense

Tuan Phan
Tuan Phan
10,824 Points

Well, as the requirement of this task, it only asks you to print out the result if the favorite_color is "blue". So...you just need to print it out after checking it right.

favorite_color = input("What is your favorite color?  ")
if favorite_color == "blue":
print("you must love the sky!")

favorite_color = input("What is your favorite color? ")

if favorite_color == "blue": print("you must love the sky!")

Robert Rydlewski
Robert Rydlewski
3,825 Points

My code doesn't work? How come?

favorite_color = input("What is your favorite color? ") if favorite_color == "blue": print("You must love the sky!")

Alan Babiarz
Alan Babiarz
3,592 Points

On 3rd line of your code, you need to make 2 spaces, then it works :)

favorite_color = input("What is your favorite color? ")

if favorite_color == "blue":

print("you must love the sky!")