212k views
20 votes
Part B

There are many different ways that a user could tell us that he or she would like to add two numbers in our calculator program. The user could type “add”, “Add”, “ADD”, or “+”, to name a few possibilities. Of course, as humans, we know exactly what is meant, even if the word is capitalized. But the Python Interpreter can’t tell that “add” is the same as “Add”.

We can use a list to make our program a bit more robust. We can also use the IN operator to check for certain values in that list. Take a look at this if statement’s opening line:

if operation in [“add”, “Add”, “ADD”, “+”]:
Make those changes in your program and verify that it works.
Consider all of the possible words the user might enter to subtract, multiply, or divide.
Rewrite the first lines of each of your if statements to use lists.
Thoroughly test your new program, trying out each of the four operations.
Share the link to your Python code in REPL.it with your teacher by clicking on the share button and copying the link.

User Iffat
by
6.1k points

1 Answer

6 votes

Answer:

The Python programming language is a great tool to use when working with numbers and evaluating mathematical expressions. This quality can be utilized to make useful programs.

This tutorial presents a learning exercise to help you make a simple command-line calculator program in Python 3. While we’ll go through one possibile way to make this program, there are many opportunities to improve the code and create a more robust calculator.

We’ll be using math operators, variables, conditional statements, functions, and handle user input to make our calculator.

Prerequisites

For this tutorial, you should have Python 3 installed on your local computer and have a programming environment set up on the machine. If you need to either install Python or set up the environment, you can do so by following the appropriate guide for your operating system.

Step 1 — Prompt users for input

Calculators work best when a human provides equations for the computer to solve. We’ll start writing our program at the point where the human enters the numbers that they would like the computer to work with.

To do this, we’ll use Python’s built-in input() function that accepts user-generated input from the keyboard. Inside of the parentheses of the input() function we can pass a string to prompt the user. We’ll assign the user’s input to a variable.

For this program, we would like the user to input two numbers, so let’s have the program prompt for two numbers. When asking for input, we should include a space at the end of our string so that there is a space between the user’s input and the prompting string.

number_1 = input('Enter your first number: ')

number_2 = input('Enter your second number: ')

After writing our two lines, we should save the program before we run it. We can call this program calculator.py and in a terminal window, we can run the program in our programming environment by using the command python calculator.py. You should be able to type into the terminal window in response to each prompt.

Output

Enter your first number: 5

Enter your second number: 7

If you run this program a few times and vary your input, you’ll notice that you can enter whatever you want when prompted, including words, symbols, whitespace, or just the enter key. This is because input() takes data in as strings and doesn’t know that we are looking for a number.

Step-by-step explanation:

Hope This helps with your coding

( If it doesn't then sorry)

User Morissette
by
7.7k points