Assignment 1: Programming Basics
This is a non-graded assignment.
Introduction: A Simple Calculator as your first computer program!
As part of this assignment, you will create and implement a computer program from scratch. By the end of the assignment, your program will function as a Simple Calculator!
On Task 1, you will implement a Simple Calculator that performs arithmetic operations based on the user's input. If you have any questions, please, ask your group tutor or attend the coaching session.
On Task 2, you can extend the functionality of your Calculator to hold and present the past results of all the performed arithmetic operations. If you have any questions, please, check out Python lists or ask your tutor.
Task 1: A Simple Calculator
Implement a simple calculator. Your calculator should provide the four basic arithmetic operations and a termination option(add, subtract, multiply, divide, end) and adhere to this specification:
The program shall ask the user to enter/select the desired operation (add/subtract/multiply/divide/end).
When the user selects an arithmetic operation, the program shall ask the user to input the two operands, calculate the result, and print the result.
The program is executed repeatedly until the user actively stops it using the command end.
Task 1.1: Retrieve the user's input (1 Point)
Let's imagine that you buy a calculator.
As a user, the first thing you would expect from your calculator is to accept two operands (e.g. 2 and 5) and an operation (e.g, "add" or "+" and "subtract" or "-"). You could also expect that there is a termination option (e.g. "end" or "off").
As a programmer, you should write the code that retrieves the input that the user expects to give.
Task 1.2: Implement a function that performs an arithmentic operation based on 2 operands and 1 operator and returns the result (3 Points)
Once the user gives their input to the calculator, they expect a result!
As a programmer, you will write a function named calculate that receives 2 operands and 1 operator as parameters. Based on the operator, the body of the function performs the corresponding arithmetic operation by using the 2 operands. At the end, the function should return the result of the arthmetic operation.
For example, if the operator is equal to "add", then the control flow should be such that the function will perform the addition(+) of the two operands. Remember to assign the returned value of the arithmetic operation to a variable (e.g. a variable named "result") and return it!
Task 1.3: Call your function by passing the user's input and print the result for the user
Right now:
you can retrieve 2 operands and 1 operator (or end) from the user,
you have a function that performs an arithmetic operation based on 2 operands and 1 operator and returns the result.
Let's link the two to make a functional calculator!
The calculator program should be executed repeatedly (check out while loops!): a) asking for the user's input and converting it to a valid datatype, b) calling the function by passing the necessary arguments and, c) printing the result so that the user can see it! The repetition should terminate when the user's input is equal to end.
Task 2: History-Extension for your Calculator (Advanced Challenge)
Extend your program with a "history": The program shall record each executed arithmetic operation using a Python List. Furthermore, it should be possible to display this list using an additional command "history" and not just "add", "subtract", "multiply", "divide" or "end". Please, consult the tutorial https://www.programiz.com/python-programming/list to learn how you can initialize a list, append a new element to a list (e.g. the result of an arithmetic operation) and print a list.