Reference no: EM132356701
LAB - FUNCTIONS
OBJECTIVES
In this lab assignment, students will learn:
- How to write functions that multiple parameters
- How to write code to pass multiple arguments to a called function
- How to write code to generate random numbers
- How to use math functions
GOALS
In this lab assignment, students will demonstrate the abilities to:
- Write functions that multiple parameters
- Write code to pass multiple arguments to a called function
- Write code to generate random numbers
- Use math functions
INSTRUCTION AND PROBLEMS
Write a Python program for each of the problems in this lab. The following is an example.
Lunch combos in the cafeteria of a community college include an entrée and a beverage. Write a program for customers to enter their choices.
Define and use the following two functions:
chooseEntree : handle choice of entrée
chooseBeverage: handle choice of beverage
Also write and use a main function to implement the mainline logic of the program.
Python program:
__author__ = 'Man-Chi Leung'
defmain():
chooseEntree()
print()
chooseBeverage()
defchooseEntree():
print('Entree:')
print('Enter 1 for fish and chips')
print('Enter 2 for burger and fries')
print('Enter 3 for fried chicken and smashed potato')
print('Enter 4 for barbecue pork')
entree = int(input('Please enter your choice: '))
if entree == 1:
print('You have chosen fish and chips')
elifentree == 2:
print('You have chosen burger and fries')
elifentree == 3:
print('You have chosen fried chicken and smashed potato')
elifentree == 4:
print('You have chosen barbecue pork')
defchooseBeverage():
print('Beverage:')
print('Enter 1 for iced tea')
print('Enter 2 for soda')
print('Enter 3 for coffee')
beverage = int(input('Please enter your choice: '))
if beverage == 1:
print('You have chosen iced tea')
elifbeverage == 2:
print('You have chosen soda')
elifbeverage == 3:
print('You have chosen coffee')
main()
Please use PyCharm to type and test your programs. Submit the Python files to Blackboard for credit. In this lab, you should submit 4 Python files, one for each problem.
PROBLEM 1
Write a program to convert US dollar to Japanese yen. This program has two functions: main and convert_to_yen. Please do the following:
(a) In the main function, ask the user to enter amount in US dollar and conversion rate.
(b) In the main function, call the convert_to_yen function. Pass the amount in US dollar and conversion rate as arguments.
(c) In the convert_to_yen function, write code to convert US dollar to Japanese yen and display the amount in Japanese yen.
The following is an example.
Enter amount in US dollar: 100
Enter conversion rate: 124.21
Equivalent amount in Japanese Yen: 12421.0
Save your Python program in a file named Lab09P1.py. Submit the file to Blackboard for credit.
PROBLEM 2
Write a program to calculate target heart rate during fitness training. This program has two functions: main and heart_rate_calculator. Please do the following:
(a) In the main function, ask the user to enter age and resting heart rate.
(b) In the main function, call the heart_rate_calculator function. Pass the ageand resting heart rate as arguments.
(c) In the heart_rate_calculator function, write code to calculate target heart rate during fitness training with the following formula:
Target hart rate = (220 - age - resting heart rate) * 0.4 + resting heart rate
Display target heart rate.
The following is an example.
Enter age: 40
Enter resting heart rate: 70
Your target heart rate is 114.0
Save your Python program in a file named Lab09P2.py. Submit the file to Blackboard for credit.
PROBLEM 3
Write a program to play the rock-paper-scissors game. A single player is playing against the computer. In the main function, ask the user to choose rock, paper or scissors. Then randomly pick a choice for the computer. Pass the choices of the player and the computer to a find_winner function, which determines and displays the outcome of the game.
The following are three examples.
Enter 1 for rock, 2 for paper or 3 for scissors: 1
You chose rock. The computer chose scissors. You won.
Enter 1 for rock, 2 for paper or 3 for scissors: 3
You chose scissors. The computer chose rock. The computer won.
Enter 1 for rock, 2 for paper or 3 for scissors: 1
Both you and the computer chose rock. Tie.
Save your Python program in a file named Lab09P3.py. Submit the file to Blackboard for credit.
PROBLEM 4
Write a program to do the following. Generate a random floating number between 12.5 and 17.5. Calculate the square root of the random number. Display both the random number and its square root.
The following is an example.
The square root of 17.325617070542933 is 4.162405202589356