During session 3
Exercises
1. Money change
Write a function called money_change which takes an amount of money in English pounds as its parameter, converts it into euros and returns the result (currency exchange rate: 1 GBP = 1.14 EUR). Call this function at least twice in the program; at least one of the calls must be based on the data asked from the user). Output the results in euros.
Example of the function call:
>>>money_change(150.5) 171.57
Example of the program output:
Please enter the amount of money in English pounds: 90 This is 102.6 EUR 500 GBP is 570.0 EUR
2. What is the name of the month?
Write a function called month_as_name that takes the number of a month as its parameter and returns the name of the corresponding month. You can reuse and modify your solution from last week.
Example of the function call:
>>>month_as_name(9) 'September'
Test your function on different inputs.
3. Date as string
Write a function called date_as_string which takes three parameters: date, month and year (all in numbers) and returns a string that represents the date in the form monthname date, year (for example September 10, 1991). Then write a program that asks the user to enter a day, a month and a year in numbers, and the program outputs the corresponding date as a string.
Hint: you can and should reuse the function from exercise 2.
Example of the function call:
>>>date_as_string(26,9,2018) 'September 26, 2018'
Example of the program output:
Enter a day: 24 Enter a number of month: 2 Enter a year: 1918 February 24, 1918
4. Budget
The Brown's are organising a party. They have invited a large number of people. Some guests have already informed about their attendance; the others haven't yet. Apart from that the Brown's need to calculate the expenses. The party budget consists of the expenses on food (10 euros per person) and a room rent (a fixed price of 55 euros regardless of the number of people). Write a program which would help calculate the maximum or minimum amount of the expenses.
First, create a function called budget. The function takes the number of people as its parameter, calculates the budget based on the provided amount of people and returns the total amount of money spent on the party. (For example, if the argument is 5, the total amount of money spent on the party is 105).
Example of the function call:
>>>budget(8) 135
Next, write a program which:
- prompts the user for the number of guests;
- prompts the user for the number of guests who have informed about their attendance;
- calculates and outputs the maximum amount of the expenses if everyone attends (invoke the function);
- calculates and outputs the minimum amount of the expenses if the ones, who have informed about their attendance, show up (invoke the function).
Example of the program output:
Please enter the number of invited people: 26 Please enter the number of people attending: 15 Maximum budget: 315 EUR Minimum budget: 205 EUR
*5. Money
The following program helps analyse if the user has enough money (savings) to buy some kilos of sugar (amount) if the price is 2 euros per kilo (price).
def enough_money(price, amount, savings): total_price = price * amount if total_price < savings: return True else: return False if enough_money(2, 4.5, 10): print('Buy') else: print('Save more money')
It is possible to make the function definition (the first 6 lines) shorter. Can you do it with 2 lines?