【问题标题】:How to get user's input in both int and float for python3 [closed]如何在python3的int和float中获取用户的输入[关闭]
【发布时间】:2018-03-13 19:56:16
【问题描述】:

如何在 python3 的 int 和 float 中获取用户的输入?

显然编程了一个计算器,但我希望用户的输入是 int 和 float,而不仅仅是 int 或 float,而是两者。这基本上就是我所关心的,将 int 更改为 int 和 float。可悲的是,Java 使用双精度。

def calculator():
    def add(x, y):
        return x + y

    def sub(x, y):
        return x - y

    def mul(x, y):
        return x * y

    def div(x, y):
        return x / y

    def power(x, y):
        return x ** y

    def modulus(x, y):
        return x % y

    print(""" -- Select Operation --
    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division
    5. Power
    6. Modulus
    """)

    choice = input("Enter 1 or 2 or 3 or 4 or 5 or 6 from the Select Operation >> ")
    while choice not in ('1', '2', '3', '4', '5', '6'):
        choice = input("Invalid Input! Please Enter 1 or 2 or 3 or 4 or 5 or 6 from the Select Operation >> ")
    print("\n")
    print("============================================================")
    print("Gathering data...")
    # How to make user input in both int and float not int alone?
    num1 = int(input("Enter First number >> "))
    num2 = int(input("Enter Second number >> "))

    if choice == '1':
        print(num1, "+", num2, "=", add(num1, num2))
    elif choice == '2':
        print(num1, "-", num2, "=", sub(num1, num2))
    elif choice == '3':
        print(num1, "*", num2, "=", mul(num1, num2))
    elif choice == '4':
        print(num1, "/", num2, "=", div(num1, num2))
    elif choice == '5':
        print(num1, "^", num2, "=", power(num1, num2))
    elif choice == '6':
        print(num1, "%", num2, "=", modulus(num1, num2))
    repeat_cal()

def repeat_cal():
    print("Do you want to Select Operation again")
    choose_again = input("Enter Y for YES or N for NO>> ")
    if choose_again.upper() == 'Y':
        calculator()
    elif choose_again.upper() == 'N':
        print("-----------------Good Bye!---------------------------")
        exit()
    else:
        repeat_cal()

calculator()

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 On topichow to ask 在这里申请。
  • 你的问题还不清楚;按照您参加的介绍性教程中的建议,请举例说明您正在尝试做的事情以及您已经尝试过的代码。

标签: python python-3.x int


【解决方案1】:

我建议您只将输入作为浮点数。这样,如果用户输入一个浮点数,你就可以支持它。

如果用户输入一个int,它也将被支持。如果出于某种原因,您想检查输入是否为 int,则只需检查该数字是否在小数点后为 0。

【讨论】:

    【解决方案2】:

    使用字符串构造函数将输入作为十进制。十进制以避免浮点精度损失。 Decimal 可以表示任意精度,包括整数,并且总是返回一个计算器预期的值。

    def calculator():
        def add(x, y):
            return x + y
    
        def sub(x, y):
            return x - y
    
        def mul(x, y):
            return x * y
    
        def div(x, y):
            return x / y
    
        def power(x, y):
            return x ** y
    
        def modulus(x, y):
            return x % y
    
        print(""" -- Select Operation --
        1. Addition
        2. Subtraction
        3. Multiplication
        4. Division
        5. Power
        6. Modulus
        """)
        from decimal import Decimal
        choice = input("Enter 1 or 2 or 3 or 4 or 5 or 6 from the Select Operation >> ")
        while choice not in ('1', '2', '3', '4', '5', '6'):
            choice = input("Invalid Input! Please Enter 1 or 2 or 3 or 4 or 5 or 6 from the Select Operation >> ")
        print("\n")
        print("============================================================")
        print("Gathering data...")
        # How to make user input in both int and float not int alone?
        num1 = Decimal(input("Enter First number >> "))
        num2 = Decimal(input("Enter Second number >> "))
    
        if choice == '1':
            print(num1, "+", num2, "=", str(add(num1, num2)))
        elif choice == '2':
            print(num1, "-", num2, "=", str(sub(num1, num2)))
        elif choice == '3':
            print(num1, "*", num2, "=", str(mul(num1, num2)))
        elif choice == '4':
            print(num1, "/", num2, "=", str(div(num1, num2)))
        elif choice == '5':
            print(num1, "^", num2, "=", str(power(num1, num2)))
        elif choice == '6':
            print(num1, "%", num2, "=", str(modulus(num1, num2)))
        repeat_cal()
    
    def repeat_cal():
        print("Do you want to Select Operation again")
        choose_again = input("Enter Y for YES or N for NO>> ")
        if choose_again.upper() == 'Y':
            calculator()
        elif choose_again.upper() == 'N':
            print("-----------------Good Bye!---------------------------")
            exit()
        else:
            repeat_cal()
    
    calculator()
    

    【讨论】:

    • 感谢 Decimal 的作品。
    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2011-12-18
    • 1970-01-01
    • 2020-09-03
    相关资源
    最近更新 更多