shahwaqas

shahwaqas

Lv3

1 Follower
0 Following
0 Helped

ANSWERS

Published47

Subjects

Astronomy1Electrical Engineering46
Answer: Wireless Local Loop (WLL) is a technology used in cellular networks to...
Answer: Step-by-step explanation:(a) To calculate the efficiency of the power ...
Answer: (a) Definitions of PV cell, PV module, and PV array:1. PV Cell (Photov...
Answer: (a) Components of a typical stand-alone (off-grid) system:1. Solar Pan...
Answer: To determine the most suitable microcontroller board for each applicat...
Answer: To design a filter for separating voice frequencies ranging between šœ‹/...
Answer: To write a simple program to control the state of 3 LEDs using the ATm...
Answer: The correct answer is b. may reduce the stability limit.Overloading an...
Answer: The correct answer is b. may reduce the stability limit.Overloading an...
Answer: An NPN transistor is a type of bipolar junction transistor (BJT) that ...
Answer: Electricity is a form of energy resulting from the presence and moveme...
Answer: Reliability refers to the ability of a system, product, or service to ...
Answer: Step-by-step explanation: Let's simplify the given expression step by ...
Answer: The IC 7483 is a 4-bit binary full adder that can perform addition of ...
Answer: Traffic Engineering plays a crucial role in the field of telecommunica...
Answer: Traffic Engineering plays a crucial role in the field of telecommunica...
Answer: Given the voltage function Vc(t) = 500te^(-2500t), we can now calculat...
Answer: To determine the total power developed in a circuit, we need informati...
Answer: The following statements are true about inflation:a) It occurs when th...
Answer: The correct answer is (ii) management of the money supply.Step-by-step...
Answer: There are several schools and colleges in the United States that offer...
Answer: To express 0.9 as a fraction, we can observe that it is a decimal numb...
Answer: Thomas Edison is often regarded as an inventor-entrepreneur rather tha...
Answer: The purpose of assistive media is to enhance accessibility and provide...
Answer: Step-by-step explanation:1. Difference between SaaS and PaaS in terms ...
Answer: Step-by-step explanation: To develop a state-variable description of t...
Answer: Analog and digital are two different types of signal representations. ...
Answer: An impulse can be increased by: A. Increasing the force: Impulse is de...
Answer: D The statement "There is an attraction between the gas molecules and ...
Answer: The high surface tension of water leads to several effects:1. Capillar...
Answer: Moment of inertia is the angular analog of mass.
Answer: In the given scenario, we have a compass placed due East of a wire. Th...
Answer: To solve the equation 124 = 3(4)^(2x-1) for x using logarithms, we can...

Hi,Ā 

I am having some issues with some code I wrote for this problem:

ā€œWrite a function namedd calc that will evaluate a simple arithmetic expression. The input to your program will be a string of the form:
operand1 operator operand2
where operand1 and operand2 are non-negative integers and operator is a single-character operator, which is either +, -, or *. You may assume that there is a space between each operand and the operator. You may further assume that the input is a valid mathemat- ical expression, i.e. your program is not responsible for the case where the user enters gibberish.
Your function will return an integer, such that the returned value is equal to the value produced by applying the given operation to the given operands.
Sample execution:
calc("5 + 10") # 15
You may not use the split or eval functions in your solution.
Hint: the hard part here is breaking the input string into its three component. You may use the find and rfind functions to find the position of the first and last space, and then use the slice operator (that is, s[startindex:endindex]) to extract the relevant range of characters. Be careful of off-by-one errors in using the slice operator.
Hint: itā€™s best to test your code as you work. The first step should be to break the input string into its three components. Write a program that does that, have it print out the operator and the two operands on separate lines, and test it until you are convinced that it works. Then, modifying it to perform the desired mathematical operation should be straightforward. Test your program with several different inputs to make sure it works as you expect.ā€

Here is my code:

def calc(exp):

Ā  Ā  operand1 = int(exp[:1])

Ā  Ā  operand2 = int(exp[4:6])

Ā  Ā  operator = exp[2:3]

Ā Ā  Ā 

Ā  Ā  if(operator == "+"):

Ā  Ā  Ā  Ā  addition = operand1+operand2

Ā  Ā  Ā  Ā  return addition

Ā Ā  Ā 

Ā  Ā  if(operator == "-"):

Ā  Ā  Ā  Ā  subtraction = operand1-operand2

Ā  Ā  Ā  Ā  return subtraction

Ā Ā  Ā 

Ā  Ā  if(operator == "*"):

Ā  Ā  Ā  Ā  multiplication = operand1*operand2

Ā  Ā  Ā  Ā  return multiplication

Ā 

print(calc("5 + 10"))

print(calc("4 - 8"))

print(calc("4 * 3"))

My code does not fully meet the criteria of this question. It only works for single digit numbers. How can I make my code work for any number?Ā 

Like:Ā 

ā€œ504 + 507ā€

ā€5678 + 76890ā€ and so on?Ā 

Ā 

Thank you. Any help is appreciated.Ā 

Ā 

Answer: To make your code work for any number, you can modify the code to hand...
Answer: To integrate AIQ and movies into your work, you can follow these steps...
Answer: Computers aid the community in numerous ways and have become integral ...
Answer: Step-by-step explanation: To calculate the current flowing through the...
Answer: Step-by-step explanation: To calculate the total output power of an AM...
Answer: e) All of the aboveStep-by-step explanation: e) All of the aboveA tran...
Answer: e) All of the aboveStep-by-step explanation: A transistor can serve mu...

Weekly leaderboard

Start filling in the gaps now
Log in