Week 2: Exercise Solutions

Exercise 2.1

Exercise 2.2

Exercise 2.3

# To calculate body mass index
height = float(input("What is your height in metres? "))
weight = float(input("What is your weight in kilos? "))
print("Your BMI is", weight / height**2)

Exercise 2.4

# To decide if a number is non-zero
number = int(input("Enter an integer: "))
print("It is", number != 0, "that your number is non-zero")

Exercise 2.5

In the first case, the condition is always true so the program will just print

Spam
Spam and eggs

over and over again forever and will never print Lobster thermidor.

In the second case the condition is never true, so will not print anything about spam but will only print Lobster thermidor.