Bethlehem Baptist Church – Python – March 18th

Thanks all for attending the first Python Philly CodoDojo meetup! Please review/repeat the Python setup, we would like to make sure that everybody is setup the same way.

Below is a quick overview of what we did and an assignment which we would like you to tackle.  Feel free to reply below with any questions or issues.

Calculator Project

Objective

Create a command line calculator in python. The calculator program should ask the user for mathematical operation (addition, subscription, division and multiplication) and input arguments.

Filename

calculator.py

Run

python calculator.py

Sample output

Please enter mathematical operation (add, sub, mul, div): add
Selected:  add
Please enter first number:  10
Please enter second number:  10
Addition result:  20

Source code

Concepts learned

  • Methods/functions
  • Command line input
  • Flow control (if, elif, else)

Project assignments

Currently, our program quits after its done with the calculation. Modify the program to keep asking the user for operation and operation arguments, after each operation add the result to the total. See sample output and hints below.

For example

(10 + 10) + (100 – 50) = 70

Sample output

Please enter mathematical operation (add, sub, mul, div) or type quit to exit the program: add
Selected:  add
Please enter first number:  10
Please enter second number:  10
Addition result:  20
Total:  20
Please enter mathematical operation (add, sub, mul, div) or type quit to exit the program: sub
Selected:  sub
Please enter first number:  100
Please enter second number:  50
Substruction result:  50
Total:  70
Please enter mathematical operation (add, sub, mul, div) or type quit to exit the program: quit
Selected:  quit

 Hints

  • Use a while loop
  • You will need to define a variable called “total”. Add the individual operation result to total.
  • The individual operation methods need to return the result so that it can be added to total

Hints Source code

1 Comment

  1. PhillyCoderDojo on March 19, 2017 at 11:01 pm

    Please let us know if you have any questions

Leave a Comment