A beginner Python exercise regarding two numbers and the ' power ' operation

101 Views Asked by At
  1. Type a function that gets an input of two whole numbers and returns their multiplication result, with no use of mathematical operations other than addition.

  2. Type a function that gets an input of two numbers, and returns the result of the first to the power of the second, mathematical operations are not allowed at all, it is allowed and even recommended to use the function from the previous exercise.

Now 18. is pretty straightforward, I did it:

def multiplication_of_numbers(first_num, second_num):

    result = 0
    for repetition in range(first_num):
        result += second_num

    return result

first_number = int(input('type in the first number > '))
second_number = int(input('type in the second number > '))

print(f'{first_number} multiplied by {second_number is {multiplication_of_numbers(first_number,second_number)}')

However I can't seem to find out how to use multiplication in order to get the power using the same two numbers. Maybe I'm missing something.

I tried to find the mathematical relation between the result of a multiplication and the result of the Exponentiation but I found none. Also couldn't figure out how to do it with "mathematical operations are not allowed at all"

3

There are 3 best solutions below

3
X3R0 On BEST ANSWER

To complete this task, you should use your multiplication_of_numbers function in a new function to get the result of an exponential expression.

def multiplication_of_numbers(first_num, second_num):
    result = 0
    for _ in range(first_num):  # it is convention to use _ if the value is not used
        result += second_num
    return result
    

def power(base, exponent):
    result = 1  # 0^n = 0 => 1 must be used instead of 0
    for _ in range(exponent):
        result = multiplication_of_numbers(result, base)
    return result


def main():
    try:
        print('type in the first number > ', end='')
        first_number = int(input())
        print('type in the second number > ', end='')
        second_number = int(input())

        print(f'{first_number}^{second_number} = {power(first_number,second_number)}')
    except ValueError:
        print('An invalid input was given.')  # In case the input cannot be converted to an integer value


if __name__ == '__main__':
    main()

3
Inereste On

Consider the two numbers x and y :

In general: What does xy (x to the power of y) mean?

It means x multiplied by itself y times

xy = x1 * x2 ... * xy

Some specific examples:

35 = 3 * 3 * 3 * 3 * 3

53 = 5 * 5 * 5

22 = 2 * 2

In code

Given your function for multiplication_of_numbers I think you can create a very similar function for a power_of_numbers function

1
Manuel Marcus On

Of course, it is much complicated, than the simple * and ** methods:

def multiplyNumbers(numberOne, numberTwo, multiplyTimes):
    result = 0
    
    for x in range(numberTwo):
        result += numberOne

    for y in range(multiplyTimes - 1):
        powerResult = 0
        for z in range(numberOne):
            powerResult += result
        
        result = powerResult
    
    return result

def power(numberOne, numberTwo):
    result = multiplyNumbers(numberOne, 1, numberTwo)
    return result

while True:
    numberOne = input("Type the first number: ")
    # This prevents outputting error if the number is not valid
    if numberOne.isnumeric() == False:
        print("Please enter a whole number.")
    else:
        numberOne = int(numberOne)
        break

while True:
    numberTwo = input("Type the second number: ")
    # This prevents outputting error if the number is not valid
    if numberTwo.isnumeric() == False:
        print("Please enter a whole number.")
    else:
        numberTwo = int(numberTwo)
        break

print(str(numberOne) + " * " + str(numberTwo) + ": " + str(multiplyNumbers(numberOne, numberTwo, 1)))
print(str(numberOne) + " ^ " + str(numberTwo) + ": " + str(power(numberOne, numberTwo)))

There is one more argument in the multiplyNumbers function, but it is still regular according to the task. It uses just addition, and the power function not uses any mathematical operations.

If it is not allowed to use three arguments in the multiplyNumbers function, you can use this code:

operation = "multiply"

def multiplyNumbers(numberOne, numberTwo):
    result = 0

    if operation == "multiply":
        multiplyTimes = 1
    else:
        multiplyTimes = numberTwo
        numberTwo = 1
    
    for x in range(numberTwo):
        result += numberOne

    for y in range(multiplyTimes - 1):
        powerResult = 0
        for z in range(numberOne):
            powerResult += result
        
        result = powerResult
    
    return result

def power(numberOne, numberTwo):
    global operation
    operation = "power"

    result = multiplyNumbers(numberOne, numberTwo)
    return result

while True:
    numberOne = input("Type the first number: ")
    # This prevents outputting error if the number is not valid
    if numberOne.isnumeric() == False:
        print("Please enter a whole number.")
    else:
        numberOne = int(numberOne)
        break

while True:
    numberTwo = input("Type the second number: ")
    # This prevents outputting error if the number is not valid
    if numberTwo.isnumeric() == False:
        print("Please enter a whole number.")
    else:
        numberTwo = int(numberTwo)
        break

print(str(numberOne) + " * " + str(numberTwo) + ": " + str(multiplyNumbers(numberOne, numberTwo)))
print(str(numberOne) + " ^ " + str(numberTwo) + ": " + str(power(numberOne, numberTwo)))

I hope this works.