如何在Python中将字符串转换为复数?

2024-09-30 16:35:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python创建一个特殊的计算器,但是要做到这一点,我应该了解更多关于复数的知识。我的计算器必须得到一个字符串并输出复方程。请帮帮我。在

myinput = "5 + 2j"
myoutput = ConvertToComplex(myinput) * (3 + 12j)  #please help me with writing this function
print(myoutput)

Tags: 字符串withhelpfunctionthis计算器me复数
1条回答
网友
1楼 · 发布于 2024-09-30 16:35:28

您要查找的函数已经内置到python中了。你只需要先去掉空格:

myinput = "5 + 2j"
mycleaninput = myinput.replace(" ","")
myoutput = complex(mycleaninput) * (3 + 12j)
print(myoutput)

相关问题 更多 >