重置前基本计算器需要多个输入

Multiple inputs are needed for a basic calculator before resetting

我的程序快完成了,但我似乎不能允许...

"Would you like to do more calculations? Enter (Y) for yes, or any ""other character for no. "

...在没有先输入我的选择(例如 "Y" 或任何其他字符”)的情况下多次输出。

Output

非常感谢您的帮助!

"""My First Program!!!"""

# Modules:

import time     # Provides time-related functions


# Delayed text to give it a "Turing" feel

def calculator_print(*args, delay=1):
    print(*args)
    time.sleep(delay)

# Operations:


def add(num1, num2):
    #   Returns the sum of num1 and num2
    return num1 + num2


def sub(num1, num2):
    #   Returns the difference of num1 and num2
    return num1 - num2


def mul(num1, num2):
    #   Returns the product of num1 and num2
    return num1 * num2


def div(num1, num2):
    #   Returns the quotient of num1 and num2
    try:
        return num1 / num2
    except ZeroDivisionError:
        # Handles division by zero
        calculator_print("Division by zero cannot be done. You have broken the universe. Returning zero...")
        return 0


def exp(num1, num2):
    #   Returns the result of num1 being the base and num2 being the exponent
    return num1 ** num2


# Run operational functions:

def run_operation(operation, num1, num2):
    # Determine operation
    if operation == 1:
        calculator_print("Adding...\n")
        calculator_print(num1, "+", num2, "=", add(num1, num2))
    elif operation == 2:
        calculator_print("Subtracting...\n")
        calculator_print(num1, "-", num2, "=", sub(num1, num2))
    elif operation == 3:
        calculator_print("Multiplying...\n")
        calculator_print(num1, "*", num2, "=", mul(num1, num2))
    elif operation == 4:
        calculator_print("Dividing...\n")
        calculator_print(num1, "/", num2, "=", div(num1, num2))
    elif operation == 5:
        calculator_print("Exponentiating...\n")
        calculator_print(num1, "^", num2, "=", exp(num1, num2))
    else:
        calculator_print("I don't understand. Please try again.")


def main():
    # Ask if the user wants to do more calculations or exit:
    def restart(response):
                    # uses "in" to check multiple values,
                    # a replacement for (response == "Y" or response == "y")
                    # which is longer and harder to read.
        if response in ("Y", "y"):
            return True
        else:
            calculator_print("Thank you for calculating with me!")
            calculator_print("BEEP BOOP BEEP!")
            calculator_print("Goodbye.")
            return False

# Main functions:

    #  Title Sequence
    calculator_print('\n\nThe Sonderfox Calculator\n\n')
    calculator_print('     ----LOADING----\n\n')
    calculator_print('Hello. I am your personal calculator. \nBEEP BOOP BEEP. \n\n')
    while True:  # Loops if user would like to restart program
        try:
            # Acquire user input
            num1 = (int(input("What is number 1? ")))
            num2 = (int(input("What is number 2? ")))
            operation = int(input("What would you like to do? \n1. Addition, 2. Subtraction, 3. Multiplication, "
                                  "4. Division, 5. Exponentiation \nPlease choose an operation: "))
        except (NameError, ValueError):  # Handles any value errors
            calculator_print("Invalid input. Please try again.")
            return
        run_operation(operation, num1, num2)
        # Ask if the user wants to do more calculations or exit:
        restart_msg = input("Would you like to do more calculations? Enter (Y) for yes, or any "
                            "other character for no. ")
        if not restart(str(input(restart_msg))):  # uses the function I wrote
            return


main()

您正在请求输入的输入。 restart_msg = input() 然后你输入(restart_msg).

另请注意,无需像 python3 input() 中那样使用 str() 将输入转换为字符串 returns 字符串。

如果这真的是您的第一个节目,那真是令人印象深刻!

所以,我把我们要关注的代码粘贴在下面:

restart_msg = input("Would you like to do more calculations? Enter (Y) for yes, or any other character for no. ")
if not restart(str(input(restart_msg))):  # uses the function I wrote
    return   # Stop the program

在第一行中,计算机提示输入 "Would you like to do more calculations?"(依此类推)。然后它将第一个输入存储在变量 restart_msg 中。然后,在第二行中,调用 restart(str(input(restart_msg)))。由于它包含对 input() 的调用并将 restart_msg 作为唯一参数传递,因此计算机会通过输出您刚刚输入的内容来提示输入。它将该条目存储在一个字符串中,并将其传递给 restart()

第二行好像是你的意图:

if not restart(str(restart_msg)):

这样,计算机通过 str() 将您输入的第一个输入转换为字符串,然后通过您的重启函数将其传递。

这是一个雄心勃勃的项目,祝你好运!