This problem and all parts within have already been completed for you as an example. Note that there are several ways to solve these problems and the solutions below are only one of them. As these problems have example solutions provided, you may not submit solutions to the problems below for marking – you must attempt one of the other provided notebooks.
Please feel free to experiment with the solutions given below to familiarise yourself with Python.
Refering to the example discussed in the lecture notes, create a program that is capable of converting a temperature in Fahrenheit to Celsius. The formula that you will need to implement is:
???????=59×(??ℎ???ℎ???−32)Celsius=59×(Fahrenheit−32)
An example of the program output is included below.
50.0 degrees fahrenheit is 10.0 degrees celsius
Use the cell below to do this. You may create more cells if you wish.
Hint: use the input([prompt])
function to ask the user to enter the temperature in degrees, then use the returned value to output the converted temperature.
In [ ]:
fahrenheit = int(input("Enter a temperature in degrees Fahrenheit:"))
celcius = (5/9)*(fahrenheit - 32)
print(fahrenheit, "degrees Fahrenheit is", celcius, "degrees Celcius")
Extend the above program so that it can perform either Celsius to Fahrenheit or Fahrenheit to Celsius conversion based on user choice. Degree Celsius can be converted to Fahrenheit based on the formula below:
???ℎ??ℎ???=95×???????+32Frahenheit=95×Celsius+32
We include three examples of the program below:
Choose an option below (1 for Celsius to Fahrenheit conversion, 2 for Fahrenheit to Celsius conversion): 1
Enter a temperature in degree Celsius: 10
10.0 degrees Celsius is equal to 50.0 degrees Fahrenheit
Choose an option below (1 for Celsius to Fahrenheit conversion, 2 for Fahrenheit to Celsius conversion): 2
Enter a temperature in degree Fahrenheit: 50
50.0 degrees Fahrenheit is equal to 10.0 degrees Celsius
Choose an option below (1 for Celsius to Fahrenheit conversion, 2 for Fahrenheit to Celsius conversion): 3
Invalid input. Bye!
Use the cell below to do this.
Hint: You will need to use a selection control structure (see the lecture slides) to determine which conversion the user wishes to perform.
In [ ]:
choice = int(input("Choose an option (1 for C to F; 2 for F to C):"))
if choice == 1:
# the user chose C to F
celcius = int(input("Enter a temperature in degrees Celcius:"))
fahrenheit = (9/5)*celcius + 32
print(celcius, "degrees Celcius is", fahrenheit, "degrees Fahrenheit")
elif choice == 2:
# the user chose F to C
fahrenheit = int(input("Enter a temperature in degrees Fahrenheit:"))
celcius = (5/9)*(fahrenheit - 32)
print(fahrenheit, "degrees Fahrenheit is", celcius, "degrees Celcius")
else:
# the user chose an unavailable option
print("Your choice is invalid.")
In this program, instead of prompting for user input, you will create a conversion table that shows the corresponding degree Fahrenheit values for degree Celsius values. Your table should start from 0 degree Celsius up to 40 degree Celsius with a step of 5.
Example of the program output is shown below.
C F
0 32.0
5 41.0
10 50.0
15 59.0
20 68.0
25 77.0
30 86.0
35 95.0
40 104.0
Use the cell below to write your program. You must not hardcode this output into your program.
Hint: You will need to use an iteration control structure to loop over a list of values and convert each to Fahrenheit. Remember that lists of numbers can be generated with the range(start, stop[, step])
function.
In [ ]:
# print the header part first. Note that t means the "tab character"
print("C", "t", "F")
# loop over a range of celcius values starting from 0 and ending at 45 increasing by 5
# note that the stop value is 45 and not 40 as the stop value is NOT included
for celcius in range(0, 45, 5):
fahrenheit = (9/5)*celcius + 32
print(celcius, "t", fahrenheit)
Modifying the solution to Part C, design a program that allows the user to enter the start value, the end value and the step for the above program.
In [ ]:
start = int(input("Input the starting temperature in Celcius:"))
stop = int(input("Input the ending temperature (exclusive) in Celcius:"))
step = int(input("Input the increment (step) value:"))
print("C", "t", "F")
for celcius in range(start, stop, step):
fahrenheit = (9/5)*celcius + 32
print(celcius, "t", fahrenheit)
In this exercise, you will write a program for calculating the interest accrued on a loan given the interest rate. There are two different schemes for interest payment. One is the simple interest scheme, which is calculated according to the formula below:
?=?×(1+?100?)y=x×(1+r100t)
where
The other is the compound interest scheme calculated according to the following formula:
?=?×(1+?100)?y=x×(1+r100)t
An example of the expected programs output is shown below:
Enter the loan amount: 300000
Enter the annual interest rate in percentage: 5
Enter the loan term in number of years: 20
Your repay amount is 600000.0 in 20.0 years of time with simple interest.
Your repay amount is 798989.311543 in 20.0 years of time with compound interest.
Use the cell below to write your solution. You may create more cells if you wish. You are not required to round your output to the nearest cent.
In [ ]:
Extend the above program to enable input validation on loan amount immediately after entering the amount. The minimum loan amount is 100,000, and the maximum is 1,000,000. Note you don’t have to validate other inputs.
Below are three examples of how the program should run.
Enter the loan amount: 300000
Enter the annual interest rate in percentage: 5
Enter the loan term in number of years: 20
Your repay amount is 600000.0 in 20.0 years of time with simple interest.
Your repay amount is 798989.311543 in 20.0 years of time with compound interest.
Enter the loan amount: 5000
The minimum loan amount is 100000. Bye!
Enter the loan amount: 2000000
The maximum loan amount is 1000000. Bye!
In [ ]:
Write a program to create a table showing the accumulation of interest each year for different interest schemes. Accumulated interest is the difference between repay amount and loan amount. Your program should calculate the interest value for each year over a 20 year period with the initial loan amount of 300,000 and interest rate of 5%.
An example of the program is below:
Year Simple Interest Compound Interest
1 15000.0 15000.0
2 30000.0 30750.0
3 45000.0 47287.5
4 60000.0 64651.875
5 75000.0 82884.46875
6 90000.0 102028.692188
7 105000.0 122130.126797
8 120000.0 143236.633137
9 135000.0 165398.464794
10 150000.0 188668.388033
11 165000.0 213101.807435
12 180000.0 238756.897807
13 195000.0 265694.742697
14 210000.0 293979.479832
15 225000.0 323678.453823
16 240000.0 354862.376515
17 255000.0 387605.49534
18 270000.0 421985.770107
19 285000.0 458085.058613
20 300000.0 495989.311543
Do not worry too much about producing perfectly aligned output as shown above. However, you should ensure the output is at least legible.
Hint: use a for loop and a list to hold the values of 1, 2, …, 20.
In [ ]:
Extend the above program so that it allows the user to enter the initial loan amount, interest rate, start year and end year for interest calculation from keyboard.
In [ ]:
Write a program to enter a student record from the keyboard. The record includes the student ID, student name, the program of study, campus of enrolment.
Example program output:
Enter your student ID: 18007007
Enter your name: James Bond
Enter your program of study: Master of ICT
Enter your campus of enrolment: Parramatta
Welcome to Western Sydney University, James Bond. You are studying Master of ICT in Parramatta.
Write your program in the cell below.
In [ ]:
Extend the program Part A to allow the users to enter student or staff information from the keyboard. The program first asks for ID and automatically determines whether this is a student or staff based on ID value. If the ID is less than 20000000, it’s a student and the program should proceed with asking for student information above. Otherwise it’s a staff member and the program should proceed with asking for staff information, including staff name, job title and school he/she works in. The program should also perform input validation so that it only accepts 8 character input for ID (there is no need to validate whether a number is entered or not)
We list some examples of the expected output below:
Enter your student ID: 18007007
Enter your name: James Bond
Enter your program of study: Master of ICT
Enter your campus of enrolment: Parramatta
Welcome to UWS, James Bond. You are studying Master of ICT in Parramatta.
Enter your student/staff ID: 300
Invalid input. You must enter a 8-digit number.
Enter your student/staff ID: 30088888
Enter your name: Andrew O'Brien
Enter your job title: Lecturer
Which school do you work for: SCEM
Welcome back, Andrew O'Brien. You are a Lecturer from SCEM.
Write your program in the cell below.
In [ ]:
Extend program Part B to allow users to enter student or staff information repeatedly from keyboard and stop when invalid student/staff ID is entered.
An example of the expected output is below:
Enter your student/staff ID: 30088888
Enter your name: Zhouyu Fu
Enter your job title: Lecturer
Which school do you work for: School of Big Data
Welcome back, Zhouyu Fu. You are a Lecturer from School of Big Data.
Enter your student ID: 18007007
Enter your name: James Bond
Enter your program of study: Master of ICT
Enter your campus of enrolment: Parramatta
Welcome to UWS, James Bond. You are studying Master of ICT in Parramatta.
Enter your student/staff ID: 300
See you later!
Write your program in the cell below.
In [ ]:
Write a program to enter three numbers from the keyboard. Your program should display user messages for input and show the numbers after user entering the numbers.
An example of the expected output is shown below:
Enter the first number: 12
Enter the second number: 18
Enter the third number: -6
The three numbers you entered are 12 , 18 , -6
Write your program in the cell below.
In [ ]:
Modify program Part A to find out the maximum number that the user has entered. Instead of displaying all input numbers, the program should just display the largest number.
An example of the expected out is shown below:
Enter the first number: 12
Enter the second number: 18
Enter the third number: -6
The largest number you entered is 18
Write your program in the cell below.
Hint: You could solve this problem by using if
statements to determine the largest number, or use a built-in Python function to work it out for you. Refer to the Python 3 documentation on Built-in Functions and see if there is a function which will help solve your problem.
In [ ]:
Extend the program Part B so that your new program will repeatedly ask the user to enter numbers and only stop if the user has entered 0. After that, your program should display the largest number that the user has entered in the loop.
An example of the expected output is shown below:
Enter a number (0 to stop): 12
Enter a number (0 to stop): 18
Enter a number (0 to stop): -6
Enter a number (0 to stop): -21
Enter a number (0 to stop): 3
Enter a number (0 to stop): 0
The largest number you entered is 18
Write your program in the cell below.
In [ ]:
In this exercise, you will write a program for calculating the bonus earned by a sales person. The bonus is equal to 7% of the sales amount. Your program should receive the sales amount value from user input and display the bonus value.
An example of the expected output is shown below:
Enter the sales amount: 6250
The bonus is 437.5
Write your program in the cell below.
In [ ]:
The company decides to introduce a progressive rule to award bonus, with different bonus rate for different levels of sales amount in the following.
Sales Amount | Bonus Rate |
---|---|
≤ 5000 | 0 |
≤ 10000 | 5% for each dollar over 5000 |
≤ 20000 | $250 + 10% for each dollar over 10000 |
> 20000 | $1250 + 20% for each dollar over 20000 |
Write a program to calculate the bonus for the given sales amount using the new rule above.
Some examples of the expected output are shown below.
Enter the sales amount: 4000
Your bonus is 0.0
Enter the sales amount: 8000
Your bonus is 150.0
Enter the sales amount: 12000
Your bonus is 450.0
Enter the sales amount: 24000
Your bonus is 2050.0
Write your program in the cell below.
In [ ]:
Extend program Part B to repeat the process of entering sales amount and calculating the bonus until a negative value is entered.
An example of the expected output is shown below.
Enter the sales amount: 4000
Your bonus is 0.0
Enter the sales amount: 8000
Your bonus is 150.0
Enter the sales amount: 12000
Your bonus is 450.0
Enter the sales amount: 24000
Your bonus is 2050.0
Enter the sales amount: -100
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more