我正在尝试构建一个执行复数基本运算的计算器。我正在使用我在网上找到的计算器的代码,我希望能够将用户输入作为复数。现在代码使用 int(input) 来获取要评估的整数,但我希望输入采用复数形式,格式为 complex(x,y),其中用户只需要为每个输入 x,y复数。我是 python 的新手,所以鼓励解释,如果这是不可能的,那很高兴知道。这是现在的代码:
# define functions
def add(x, y):
"""This function adds two numbers"""
return x + y
def subtract(x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(x, y):
"""This function multiplies two numbers"""
return x * y
def divide(x, y):
"""This function divides two numbers"""
return x / y
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = input("Enter choice: 1, 2, 3, or 4: ") …Run Code Online (Sandbox Code Playgroud)