Homework 3
The deadline is Thursday, the 22nd of September, 23:59 (Estonian time).
Homework 3.1 Divisibility 2
Rewrite the is divisible by 3 and 5 program using a function called is_divisible. The function takes 2 arguments - the numbers that the user input will be checked with for division (see examples below).
Examples of function calls:
>>> is_divisible(2,7)
Enter a number: 14
Number 14 is divisible by 2 and 7
>>> is_divisible(3,5)
Enter a number: 9
Number 9 is divisible by 3
>>> is_divisible(3,5)
Enter a number: hello world
The inserted value was not a number
>>> is_divisible(1,2)
Enter a number: 4
Number 4 is divisible by 1 and 2
>>> is_divisible(1,2)
Enter a number: 4
Number 4 is divisible by 1 and 2
Test the function with various input values.
If you have been struggling with this exercise for some time, then maybe you can get help from the troubleshooter which tries to explain most common problems and give hints.
Homework 3.2 Customer card profitability
Write a function called customer_card_discount that calculates if it would be profitable to buy a customer card for a discount or not (meaning that the purchase cost after discount + card price is less or equal to the original cost). The function should take the following arguments:
- total purchase amount in euros
- discount percent with customer card
- customer card price
Use the following formula to calculate the price with the discount and the card price:
new price = (total purchase amount) * (100 - discount)/100 + (customer card price)
If the card saves the customer some x amount of euros then the function should return x. If purchase + card price is more expensive than the original purchase price then return -x, where x is the number of euros it’s cheaper with the card (see examples below).
Examples of function calls:
>>> customer_card_discount(200, 5, 5)
5.0
>>> customer_card_discount(200, 0, 5)
-5.0
>>> customer_card_discount(130, 10, 2)
11.0
>>> customer_card_discount(130, 5, 10)
-3.5
Modify the program so that it asks the user for the total purchase amount in euros, discount percent, and customer card price. The program has to compare the prices and tell the user if it’s a good idea to get the card.
Examples of program output:
>>> %Run solution.py
Total purchase amount: 200
Discount percent: 5
Customer card price: 5
It’s better to get a card, you will save 5 euros.
>>> %Run solution.py
Total purchase amount: 100
Discount percent: 5
Customer card price: 15
It’s better to pass on the customer card, it will be 10 euros cheaper.
Hint
Save the price discount amount (returned by the function) in a variable. You can then check that value using if-else to figure out whether the customer card would be profitable.
If you have been struggling with this exercise for some time, then maybe you can get help from the troubleshooter which tries to explain most common problems and give hints.
Go to Moodle and upload your solution into Homework 3.