Introduction
Conditional statements
Summary

Instruction

Good. You can also execute more than one line of code in if statements:

answer1 = input('How many centimeters are there in a meter?')
if int(answer1) == 100:
  print('Very good!')
  print('Thank you!')
else:
  print('Not really...')
  print('Try harder!')

As you can see, there is more than one line after if and else, but every line that follows must keep the same indentation. The first line of code after if has 2 spaces of indentation. The second line after if also has 2 spaces of indentation. Together, those two lines below if make up a code block.

The first code block is executed when the if condition is True (user answer is correct). The second code block is executed when the if condition is False (user answer is incorrect). All lines in a code block in Python must have the same indentation level.

Exercise

Write a program that will ask the user whether they are registered. If the user answers y:

  1. Ask the user for their username: Please provide your username: . Then, show the following greeting: Welcome back, {username}!.
  2. If the user answers anything else, show the following: Sorry, the system does not accept new users.

Stuck? Here's a hint!

Use the following condition in your if statement:

if answer == 'y':
  ...
else:
  ...