Question

In: Computer Science

In GoogleCollab (Python) Write a function power(x,i) that takes an int i and a float (i.e....

In GoogleCollab (Python)

  • Write a function power(x,i) that takes an int i and a float (i.e. real number) x and returns xi, do not use Python’s in-built power function x**i , instead write a loop to do so using only multiplication.
  • Now do use the power function x**i to write a second function unit_test(y) that tests whether your first function is working correctly or not.  
  • Does power(x,i) work for any value of x and i ? For which values x and i does it throw an error? What happens if the input variables are letters instead of a float and an int?
  • Write a new function power_2(x,i)so that instead of throwing an error, the function prints a message that explains what went wrong in each case?  

Solutions

Expert Solution

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Code:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function power(x,i):

def power(x, i):

n = 1

for q in range(1, i+1):

n = x*n

return n

function unit_test(x,i):

def unit_test(x, i):

res1 = pow(x, i)

print(res1)

res2 = power(x, i)

print(res2)

if(res1 == res2):

print("Matching")

else:

print("Not matching")

It works fine with any value of x and i.

It throws an error when we try to enter any letter.

function power_2:

def power_2():

try:

x = float(input("Enter x:"))

i = int(input("Enter i:"))

unit_test(x, i)

except:

print("Please enter int and float values only")

Complete code:

def power(x, i):

n = 1

for q in range(1, i+1):

n = x*n

return n


def unit_test(x, i):

res1 = pow(x, i)

print(res1)

res2 = power(x, i)

print(res2)

if(res1 == res2):

print("Matching")

else:

print("Not matching")

def power_2():

try:

x = float(input("Enter x:"))

i = int(input("Enter i:"))

unit_test(x, i)

except:

print("Please enter int and float values only")

power_2()

Screenshot of code in colab:

output:


Related Solutions

Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints a message with information about the total amount owed and how much the tip was. As a reminder, the tip amounts are 10%, 15% and 20% for stingy, regular, and generous customers. And the tax amount should be 7%. The total amount is calculated as the sum of two amounts: check_amount = base_cost*1.07 tip_amount = tip_percentage*check_amount To receive full credit, you must use string...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a...
'PYTHON' 1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and...
Using the Python Program. Can an array hold a mixture of types i.e. int, float, string,...
Using the Python Program. Can an array hold a mixture of types i.e. int, float, string, array within the same array? Show a code segment to remove the last element from a 15 element array named myStuff[]. Please explain the code.
Python: Write a function named power() that calculates rootpow recursively. It receives two arguments root (float)...
Python: Write a function named power() that calculates rootpow recursively. It receives two arguments root (float) and pow (int) > = 1, and it returns the result root raised to power pow. The recursion of the power function is defined as: Base case: rootpow = root, if pow = 1 Recursive case: rootpow = root* rootpow-1 , otherwise. b) Develop a greatestC( ) function that determines the greatest common divisor of two integer numbers. Test greatestC() in a simple main...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
In python of Jupiter notebook Write a python function called trng that takes three numbers x,...
In python of Jupiter notebook Write a python function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangleotherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
Write a C function hwkOneA, that takes a long int x as well as two integers...
Write a C function hwkOneA, that takes a long int x as well as two integers n and m as arguments, and returns a long int. Here is the function declaration: long int hwkOneA (long int x, int n, int m); The function should swap nibble n and m of a long int x (64-bit integer). A nibble is a four-bit aggregation. For this problem, the index of the most significant nibble is 0, and the index of the least...
REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] );...
REQUIREMENTS: Write a function that matches the following declaration: int InRectangle( float pt[2], float rect[4] ); Argument pt[2] defines a point on the plane: pt[0] is the x-coordinate, pt[1] is the y-coordinate. Argument rect[4] defines a rectangle on the same plane. rect[0] and rect[1] define the x- and y- cordinates respectively of one corner of the rectangle. rect[2] and rect[3] define the opposite corner. Coordinates may be any valid floating point value, including negative values. The function returns int 0...
Python: How would I write a function that takes a directory and a size in bytes,...
Python: How would I write a function that takes a directory and a size in bytes, and returns a list of files in the directory or below that are larger than the size. For example, I can use this function to look for files larger than 1 Meg below my Home directory.
Python: How would I write a function that takes a URL of a webpage and finds...
Python: How would I write a function that takes a URL of a webpage and finds the first link on the page? The hint given is the function should return a tuple holding two strings: the URL and the link text.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT