1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
tiny-mole [99]
3 years ago
12

Write a program that will read and evaluate a mathematical expression. The expression is of the form: oprand1 op operand2, where

operand1 and operand2 are positive integers and op is an operator, which is either +, -, * or /. For example: 24 + 65 and 276 * 2 are legal expressions. Assumption: There is a single space between each operand and the operator.
Computers and Technology
1 answer:
Vedmedyk [2.9K]3 years ago
7 0

Answer:

ques=(input("Enter question "))

br = ques.split()

operand1= int(br[0])

operand2= int(br[2])

op= br[1]

if (operand1 >0 and operand2 >0):

   if op == '+':

       print(operand1, "+", operand2, "=",operand1 + operand2)

   elif op == '-':

       print(operand1, "-", operand2, "=",operand1 - operand2)

   elif op == '*':

       print(operand1, "*", operand2, "=",operand1 * operand2)

   elif op == '/':

       print(operand1, "/", operand2, "",operand1 / operand2)

   else:

       print("Please enter  a correct operator")

else:

       print("Please ener a positive number")

Explanation:

ques=(input("Enter question "))

This line prompts for an input like 2 + 23

br = ques.split()

The split function takes the input and breaks it into different parts and places it in a list with each element having an index number starting from zero

operand1= int(br[0])

operand2= int(br[2])

op= br[1]

we use the index numbers to access their location and assign them to operand1 operand2 and op(which is the operator). here we assign operand1 and operand2 to an integer data type and leave op as a string.

if (operand1 >0 and operand2 >0):

The if statement here ensures that both operand1 and two are positive.

   if op == '+':

       print(operand1, "+", operand2, "=",operand1 + operand2)

   elif op == '-':

       print(operand1, "-", operand2, "=",operand1 - operand2)

   elif op == '*':

       print(operand1, "*", operand2, "=",operand1 * operand2)

   elif op == '/':

       print(operand1, "/", operand2, "",operand1 / operand2)

<em>checks the operators, carries out the calculation and prints the solution.</em>

   else:

       print("Please enter  a correct operator")

the else statements handles all possible errors that may occur when writing the operator

else:

       print("Please enter a positive number")

This prompts the user to enter a positive number

Another approach would be to create functions for each operator and call those functions whenever the operator is inputted.

You might be interested in
To adjust the height of cells, position the pointer over one of the dividing lines between cells. When the pointer changes to th
Sergio [31]

Answer:

double arrow shape

Explanation:

To adjust the height of the cells

1. We have to position the mouse pointer over one of the column line or the one of the row line.

2. As we place the pointer between the dividing lines, the cursor of the mouse pointer change from singe bold arrow to double arrow symbol.

3.Now press or click the left mouse button and drag the dividing lines of the   cells to the desired position to have the required width or height of the cell.

5 0
4 years ago
Ummm, I hate to interrupt your day, but does anyone know where Mitch72 is????? Because he hasn't talked in a week, and he hasn't
Anettt [7]

Answer:

sorry but no

Explanation:

4 0
3 years ago
John's boss asks for a recommendation for connecting the company network to a small satellite building about 1 km from the main
tresset_1 [31]

Answer:

C. Ethernet over multi mode fiber .

Explanation:

Given that the distance to be covered is 1 km. Multi-mode optic fiber is best suited for short distances of up to 2 km and can withstand harsh environments with less signal attenuation. Single mode fiber is best suited long distance of up to 200 km or more and is expensive as compared to multi-mode fiber Ethernet over twisted pair is best for indoor use and can only cover very short distances. Although UTP and STP are relatively cheap they cannot withstand harsh environments without great signal loss.

4 0
4 years ago
Please help!!!! will mark brainliest!!
fgiga [73]

Answer:

1. Speaking or texting on the cell phone while driving.

= It cause an accident.

2. Using technology for bullying others.

Hope this answer will help you.

7 0
3 years ago
Read 2 more answers
The undo function allows the user to cancel up to _____ previous typing actions
valina [46]
It can cancel up to 40 times.
4 0
4 years ago
Read 2 more answers
Other questions:
  • You have created a number of different documents using several applications including word, excel, and powerpoint. these files a
    10·1 answer
  • While at work, Joe asked Dana to show him how to change the copier paper. Joe kept working as she explained the process, and the
    5·2 answers
  • Which of the following is the final fate for average sized stars?
    12·1 answer
  • __________________ fonts use binary code to define each pixel of the letter to be displayed.
    8·1 answer
  • What is the size of the program counter for an avr that has a 1 kbyte rom capacity?
    9·1 answer
  • HELP WILL GIVE BRAINIEST IF YOU HELP ME.....
    8·2 answers
  • To access documents stored on a "cloud", you'll
    12·1 answer
  • g How safe is to have a LinkedIn account where you have published all the important information about yourself
    13·2 answers
  • Which device allows users to directly hear data from a computer
    5·1 answer
  • Using a microphone to record a sound on your computer is an example of:
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!