如何在int和float中为python3获取用户的输入?
显然编写了一个计算器,但我希望用户的输入在int和float中,不仅仅是int或float,而且两者都是。这基本上都是我所关心的,将int更改为get 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()
答案 0 :(得分:2)
我建议你把输入作为浮点数。这样,如果用户输入浮动,您可以支持它。
如果用户输入了一个int,它也会受到支持。如果由于某种原因,您想要检查输入是否为int,则可以检查该数字在小数点后是否为0。
答案 1 :(得分:0)
使用字符串构造函数将输入作为decimal.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()