在这段代码中,我首先向用户询问三角函数,然后询问弧度或度数的角度。根据代码,控制台应该打印错误,但是在运行代码时,第一个if语句接受任何输入为true。最终的coputed值也是worng。请提出建议以及可以对代码进行的任何其他相关更改。
from math import *
trigFunc = str(input("What function do you want to use? Sine, Cosine or Tangent: "))
if trigFunc.lower == "sine" :
radOrDeg = str(input("Do you want to input the value as radians or degrees? "))
if radOrDeg.lower == "radians" :
angle = int(input("Please input the value of the angle: "))
print(math.sin(angle))
elif radOrDeg.lower == "degrees" :
angle = int(input("Please input the value of the angle: "))
radAngle = int(math.radians(angle))
print(math.sin(radAngle))
else:
print("error")
elif trigFunc.lower == "cosine" :
radOrDeg = str(input("Do you want to input the value as radians or degrees? "))
if radOrDeg.lower == "radians" :
angle = int(input("Please input the value of the angle: "))
print(math.cos(angle))
elif radOrDeg.lower == "degrees" :
angle = int(input("Please input the value of the angle: "))
radAngle = int(math.radians(angle))
print(math.cos(radAngle))
else:
print("error")
elif trigFunc.lower == "tangent" :
radOrDeg = str(input("Do you want to input the value as radians or degrees? "))
if radOrDeg.lower == "radians" :
angle = int(input("Please input the value of the angle: "))
print(math.tan(angle))
elif radOrDeg.lower == "degrees" :
angle = int(input("Please input the value of the angle: "))
radAngle = int(math.radians(angle))
print(math.tan(radAngle))
else:
print("error")
else:
print("ERROR, the function cannot be used")
input("press enter to exit")
.lower
是一个函数,你需要调用它来返回一个字符串。现在,您正在将函数与将返回False
的字符串进行比较。
将trigFunc.lower
改为trigFunc.lower()
。
您的代码中存在多个错误。以下是为了让您的代码正常工作而更改的内容:
trigFunc.lower()
而不是trigFunc.lower
。您需要调用方法来获得所需的结果。import math
而不是from math import *
,因为你始终引用math
库。math.radians(int(angle))
而不是int(math.radians(angle))
,因为math.radians
不接受字符串作为输入。