如何获得用户';python3的int和float中的s输入
如何为python3获取用户的int和float输入 显然是编了一个计算器,但我希望用户的输入是int和float,不仅仅是int或float,而是两者。这基本上就是我所关心的,更改int以获得int和float。遗憾的是,Java使用了double如何获得用户';python3的int和float中的s输入,python,python-3.x,int,Python,Python 3.x,Int,如何为python3获取用户的int和float输入 显然是编了一个计算器,但我希望用户的输入是int和float,不仅仅是int或float,而是两者。这基本上就是我所关心的,更改int以获得int和float。遗憾的是,Java使用了double 我建议您将输入作为一个浮点数。这样,如果用户输入浮点值,您就可以支持它 如果用户输入一个int,它也将被支持。如果出于某种原因,您想检查输入是否为int,您可以只检查数字在小数点后是否有0。使用字符串构造函数将输入作为decimal.decimal
我建议您将输入作为一个浮点数。这样,如果用户输入浮点值,您就可以支持它
如果用户输入一个int,它也将被支持。如果出于某种原因,您想检查输入是否为int,您可以只检查数字在小数点后是否有0。使用字符串构造函数将输入作为decimal.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()
欢迎来到StackOverflow。请阅读并遵循帮助文档中的发布指南。你的问题还不清楚;正如你在介绍之旅中所推荐的,请举例说明你正在尝试做什么,以及你已经尝试过的代码。谢谢你的帮助。
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()