[Python] How to get an exponential operation to print properly in Python? [closed]

Discussão em 'Python' iniciado por Stack, Maio 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm building a calculator app and it includes a feature where the user can find the nth root of a base number.

    Here's the relevant parts of my code for this question:

    def numberEntry1():
    entry = float(input("Enter the first number:\n"))
    return entry

    def numberEntry2():
    entry = float(input("Enter the second number:\n"))
    return entry

    def exponentialRoot(n1, n2):
    return n1 ** (1/n2)


    mathDict = {
    1: add,
    2: subtraction,
    3: multiplication,
    4: division,
    5: exponentialPower,
    6: exponentialRoot,
    }


    elif operationChoice == 6:
    if choice1 < 0:
    print(f"Evaluating the nth root of a negative number is not supported by this calculator.")
    print(f"Try again!")
    continue
    result = mathDict[6](choice1, choice2)
    if str(choice2)[-1] == "2":
    print(f"The {choice2}nd root of {choice1} equals {result}")


    Basically what I'm trying to do here is to make the code print a statement depending on what the last character of choice2 is after it's converted into a string. For example, if choice 2 is "24" I want the print statement to say "The 24th root of {choice1} equals {result}" and if choice 2 is "22" I want it to say "The 22nd root of {choice1} equals {result}"

    My current code just ends up not executing the print statement at all even though it still successfully executes the operation in the exponentialRoot function and returns the result.

    Continue reading...

Compartilhe esta Página