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



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

"您想做更多的计算吗?要输入(y),或任何"否"字符。

...从不先进入我的选择(例如" y"或任何其他字符")的输出多次。

输出

我真的很感谢您的帮助!

"""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('nnThe Sonderfox Calculatornn')
    calculator_print('     ----LOADING----nn')
    calculator_print('Hello. I am your personal calculator. nBEEP BOOP BEEP. nn')
    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("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_msg = input(),然后您进行输入(restart_msg)。

还要注意,无需像python3 Input()中的str()将输入转换为字符串。

相关内容

  • 没有找到相关文章

最新更新