Homework for Week 3
Conditional execution
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, best attempt counts.
Examples
This week we are into if-else and try-except statements. In real world, programs are developed by mixing these construcs (and others that we'll learn in the future). For example, you can write a program that asks for an input, checks if it is valid, and depending on value entered, chooses what to output.
Example 1
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!")
Example 2
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)
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.
Here are some examples of program output.
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 user's annual income (nonnegative 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.
Example
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 autograder in Moodle, under Homework for Week 3.
NB! Use home1.py and home2.py as the names of your files.