Starting from:

$29

Homework 2 Python program


Problem Statement
We would like a program that simulates a simple calculator. It should be able to add, subtract, multiply, and divide two
operands and display the result of applying the operation to the operands. It should repeatedly perform a computation
until the user chooses to quit.
Assignment
Write a Python program that does the following repeatedly: present the user with a menu, asks the user for an operation
and two operands, computes the result of doing the operation on the operands, and displays the result.
Your program must define and use at least three functions:
• getOperation() ­ this function prints out the menu of operations, asks the user for their choice, and returns
the user's choice of operation.
• computeResult(operation, operand1, operand2) ­ this function receives an operation and two
operands, computes the result of applying the operation to the operands, and returns the result. Note that division
by 0 will cause a run­time error and crash the program. The function should check that the second operand of a
division operation is not 0 before computing the result. If the second operand for a division is 0, the function
should return 0.
• main() ­ this function encompasses the main program. It should have the main menu­driven loop that
repeatedly asks the user for an operation and operands, then computes and displays the result. It should use the
other two functions.
Your program must interact with the user in exactly the manner shown in the sample run on the back page, stopping when
the user enters q for the chosen operation.
Coding Notes
• To make a Python file with a main function into a program that runs, put main() at the end of the program file.
This will call the main function, so the program will run when the module is run. Otherwise, to run the program,
type main() at the interpreter prompt.
• Use the raw_input function to ask the user for characters and strings. Use the plain input function to ask the
user for numbers.
How to Submit
The submission system will not be ready to accept assignment until class time on Monday, November 10.
Make sure to write a comment (lines starting with #) at the top of the program file with your name and the phrase
"CS 101 Homework 2" with exact spacing, capitalization and spelling. This will be used to make sure you submit the
correct program file. Save your program file and make sure it still runs.
In a web browser, go to URL submission.evansville.edu. Your login name is your ACENET username (unless you also
are in CS 210, then your login name has "­cs101" appended to it). If you didn't change your password, it is your student
ID with the leading 0. It is recommended that you change your password.
Click on the Submit Solution link for Homework 2, then click on the Browse button. Browse to your Homework 2
program file. Right­click on the program file, select Send to, then select Compressed (zipped) folder. Double­click on the
11/4/2014 Page 1 of 2 D. Hwang
ZIP folder, then click on the Submit button. (If you are using a Mac, you need to compress your program before going to
the submission system, and just choose the zipped folder.)
The submission system checks for the "CS 101 Homework 2" comment. It must be exactly as shown including spacing,
capitalization and spelling. When this comment is not found, the submission will fail. Correct the comment, save the
program file, then zip and submit it again.
Sample Run
(shown in two columns to save space, user input shown in bold)
SIMPLE CALCULATOR
Choose an operation:
+ : Add two numbers
- : Subtract two numbers
* : Multiply two numbers
/ : Divide two numbers
q : Quit
Enter your choice: +
Enter a number: 5
Enter another number: 7
5 + 7 = 12
Choose an operation:
+ : Add two numbers
- : Subtract two numbers
* : Multiply two numbers
/ : Divide two numbers
q : Quit
Enter your choice: -
Enter a number: 5
Enter another number: 7
5 - 7 = -2
Choose an operation:
+ : Add two numbers
- : Subtract two numbers
* : Multiply two numbers
/ : Divide two numbers
q : Quit
Enter your choice: *
Enter a number: 5
Enter another number: 7
5 * 7 = 35
Choose an operation:
+ : Add two numbers
- : Subtract two numbers
* : Multiply two numbers
/ : Divide two numbers
q : Quit
Enter your choice: /
Enter a number: 5
Enter another number: 7
5 / 7 = 0
Choose an operation:
+ : Add two numbers
- : Subtract two numbers
* : Multiply two numbers
/ : Divide two numbers
q : Quit
Enter your choice: /
Enter a number: 5
Enter another number: 0
5 / 0 = 0
Choose an operation:
+ : Add two numbers
- : Subtract two numbers
* : Multiply two numbers
/ : Divide two numbers
q : Quit
Enter your choice: q
All done

More products