python - 我在 python 中的简单计算器不适用于负数
问题描述
我正在尝试在 python 中编写一个非常简单的计算器。问题是当我给出正数时它工作得很好但是当我输入负数时我没有得到任何答案......我不知道为什么。如果有人可以帮助我,我将不胜感激:)。这是我的代码:
print("Hi :) This is your simple calculator")
a = float(input("please enter the first number: "))
b = float(input("please enter the second number: "))
print(f"{a}+{b} is {round((a)+(b), 3)}, {a}-{b} is {round(a-(b), 3)}, {b}-{a} is {round(b-(a), 3)}, {a}*{b} is {round(a*(b), 3)}, {a}/{b} is {round(a/(b), 3 )}, {b}/{a} is {round(b/(a), 3)}, {a} to the power of {b} is {round((a)**(b), 3)}, {b}to the power of{a} is {round((b)**(a), 3)}, {b} root of {a} is {round((a)**(1/b), 3)}, {a} root of {b} is {round((b)**(1/a), 3)} ")
解决方案
这是修改后的打印语句(没有根)。这部分似乎工作。
print(f"{a}+{b} is {round((a)+(b), 3)}, {a}-{b} is {round(a-(b), 3)}, {b}-{a} is {round(b-(a), 3)}, {a}{b} is {round(a * b, 3)}, {a} to the power of {b} is {round(a ** b, 3)}, {b}to the power of{a} is {round(b ** a, 3)}")
我把根去掉是因为你会遇到负数根的问题。对于复数,请查看cmath模块。
推荐阅读
- hibernate - 无法修改持久方法中的列 - 休眠触发器
- python - 导入 ibm_db 在命令提示符中不起作用
- css - CSS:尝试显示隐藏的文本框时出现悬停问题
- opencv - 使用网络摄像头实现 sfm/slam
- appium - 在视图中找不到 UIButton - Appium
- ios - IOS/Objective-C:删除 2018 年发布和构建设置的 NSLog 语句
- windows - 如何在 Windows 下将 stdout 和 stderr 都 freopen() 转换为单个输出文件
- mongodb - 在 mongodb 中二级展开后如何进行匹配?
- android - 如何在不重新启动应用程序的情况下发送崩溃报告
- javascript - 简单的 Javascript 删除类