【发布时间】: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 topic 和 how to ask 在这里申请。
-
你的问题还不清楚;按照您参加的介绍性教程中的建议,请举例说明您正在尝试做的事情以及您已经尝试过的代码。
标签: python python-3.x int