Homework for Week 3
After this week you can
- Use different kinds of branching to execute or skip a part of the code
- Combine conditional statements into more complex conditional structures
- Use try-except to handle runtime errors
- Add the correct amount of indentation into the statements
Conditional execution
Conditional statements allow the computer to execute parts of code conditionally: depending on whether some condition is true or false, a block of commands is executed or skipped. Typical conditions are comparisons, which check, for example, whether one value is less or greater than another. In Python, there are different forms of conditional statements used in different situations, and they can be flexibly combined with each other.
Watch the videos about how Python executes some statements and skips others:
Slides in English
Textbook in English
Quiz
Go to Moodle and solve the third quiz about video lectures. You can solve the quiz several times, the best attempt counts.
Examples
This week we are into if-else and try-except statements. These constructs (and others that we'll learn in the future) can be mixed with each other: for example, we can write a program that asks for an input, checks if it is valid, and, depending on the value entered, chooses what to output.
Age group
Write a program that gets the age of the user and prints into what age group the user belongs to. The program must work correctly even if the user enters something else than a number for the age.
age_as_string = input("How old are you? ") try: # Try to convert the input to an integer age = int(age_as_string) except: # Conversion failed, let age be -1 in this case age = -1 if age < 0: print("This is not a valid age") elif age < 18: print("You are a minor") elif age < 100: print("You are an adult") else: print("Wow, you need a lot of birthday candles!")
Here, try and except are used to test the user input for validity. If the age cannot be converted to an integer, then the program sets the age equal to -1. Therefore, in the following, it may assume that the variable age always has a numeric value and process it with the ordinary if - elif - else statements.
Number comparison
Write a program that prompts the user for two integers and then displays a message saying which of them is greater.
try: x = int(input('Enter the first number: ')) y = int(input('Enter the second number: ')) except: print('Please enter a number') else: # If both int coversions were successful, then continue here if x == y: print('Numbers are equal') else: if x < y: print(x, 'is less than', y) else: print(x, 'is greater than', y)
If the conversion to integer fails, then the program prints a message and terminates. Note the else part of the try-except block. That will be executed when the conversion succeeds.
Exercises
1. Name of the month
Write a program that asks the user to enter a month number and then prints the name of the month.
If the user enters an integer outside the range 1...12, then the programs should print a message informing about that. You can assume that the user enters only integers.
Test your program with different inputs.
Examples
Enter month number: 2
February
Enter month number: 0
Month number must be in the range [1-12]
Enter month number: 13
The year has only 12 months
2. Tax-free income
The amount of tax-free income in Estonia currently depends on the annual income:
- for annual income up to 6000 euros, tax-free income is equal to annual income
- for annual income from 6000 euros to 14400 euros, tax-free income is 6000 euros
- for annual income from 14400 euros to 25200 euros, tax-free income is computed according to the formula 6000 – 6000 ÷ 10800 × (annual_income – 14400)
- for annual income over 25200 euros, tax-free income is 0 euros.
Write a program that asks for the user's annual income (a non-negative floating-point number) and computes and outputs the tax-free income rounded to two decimal places.
If the user enters a non-numeric value as the annual income, then the program prints a corresponding message (use try-except). Also, the program prints a message if the user enters a negative value as the annual income.
Examples
Enter your annual income: 16825
Your tax-free income is 4652.78 euros
Enter your annual income: large
Please enter a number!
Enter your annual income: -100
Please enter a non-negative value!
Hint
>>> round(16.6333, 2) 16.63
Submit your solutions
Submit your solutions of exercises 1 and 2 to the autotester in Moodle, under Homework for Week 3.
NB! Use home1.py and home2.py as the names of your files.