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

2024-04-27 04:00:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序几乎完成了,但我似乎不能允许。。。你知道吗

“你想多做些计算吗?输入(Y)表示“是”,或输入任何“”其他字符表示“否”

…通过多次输入我的选择(例如“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()

Tags: andofthetoinputreturnifdef
2条回答

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

因此,我粘贴了下面我们要关注的代码:

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

在第一行,计算机提示输入“你想做更多的计算吗?”(以此类推)。然后将第一个输入存储在变量restart_msg中。然后,在第二行中,调用restart(str(input(restart_msg)))。因为它包含对input()的调用并将restart_msg作为唯一的参数传递,所以计算机通过输出您刚才输入的内容来提示输入。它将该条目存储在字符串中,并将其传递给restart()。你知道吗

似乎这是你在第二行的意图:

if not restart(str(restart_msg)):

这样,计算机通过str()将您输入的第一个输入转换为字符串,并通过重新启动函数将其传递给您。你知道吗

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

你在请求一个输入的输入。restart\u msg=input(),然后执行输入(restart\u msg)。你知道吗

另外请注意,没有必要使用str()将输入转换为字符串,因为python3 input()中已经返回了一个字符串。你知道吗

相关问题 更多 >