Python 将变量传递到函数中
这是我的代码,使用三角学来查找三角形上缺少的边。它通过使用SOHCAHTOA找到这些边来工作。 代码的问题是,当我试图运行代码时,调用用户定义的函数:get_方法时,它会给出一个无效的语法。 请告诉我代码有什么问题,以及如何解决我的程序中出现的一致性问题Python 将变量传递到函数中,python,trigonometry,Python,Trigonometry,这是我的代码,使用三角学来查找三角形上缺少的边。它通过使用SOHCAHTOA找到这些边来工作。 代码的问题是,当我试图运行代码时,调用用户定义的函数:get_方法时,它会给出一个无效的语法。 请告诉我代码有什么问题,以及如何解决我的程序中出现的一致性问题 import math def get_method(question, method): print("Lable the right-angle triangle.") question = input("What are
import math
def get_method(question, method):
print("Lable the right-angle triangle.")
question = input("What are you trying to find? Type 'h' for hypotenuse, 'a' for adjacent or 'o' for opposite.").lower().strip()
method = ""
if question == "h":
opposite_verify = input("Do you have the length of the opposite?\n"
"Type 'yes' or 'no'").strip().lower()
if opposite_verify == "yes":
method = "sine"
elif opposite_verify == "no":
adjacent_verify = input("Do you have the length of the adjacent?\n"
"Type 'yes' or 'no'").strip().lower()
if adjacent_verify == "yes":
method = "cosine"
else:
print("There's no way to solve this.")
elif question == "a":
hypotenuse_verify = input("Do you have the length of the hypotenuse?\n"
"Type 'yes' or 'no'").strip().lower()
if hypotenuse_verify == "yes":
method = "cosine"
elif hypotenuse_verify == "no":
opposite_verify = input("Do you have the length of the opposite?\n"
"Type 'yes' or 'no'").strip().lower()
if opposite_verify == "yes":
method = "tan"
else:
print("There's no way to solve this.")
elif question == "o":
hypotenuse_verify = input("Do you have the length of the hypotenuse?\n"
"Type 'yes' or 'no'").strip().lower()
if hypotenuse_verify == "yes":
method = "sine"
elif hypotenuse_verify == "no":
adjacent_verify = input("Do you have the length of the adjacent?\n"
"Type 'yes' or 'no'").strip().lower()
if adjacent_verify == "yes":
method = "tan"
else:
print("There's no way to solve this.")
return method, question
def main(question, method):
angle = float(input("What is the degrees of the angle?"))
angle /= (math.pi / 180)
if method == "sine" and question == "h":
opposite = float(input("What is length of the opposite angle?"))
hypotenuse = opposite / (math.sin(angle))
print("The length of the hypotenuse is {}".format(hypotenuse))
elif method == "sine" and question == "o":
hypotenuse = float(input("What is the length of the hypotenuse?"))
opposite = hypotenuse * (math.sin(angle))
print("The length of the opposite is {}".format(opposite))
elif method == "cosine" and question == "a":
hypotenuse = float(input("What is the length of the hypotenuse?"))
ajacent = hypotenuse * (math.cos(angle))
print("The length of the ajacent is {}".format(ajacent))
elif method == "cosine" and question == "h":
ajacent = float(input("What is the length of the ajacent?"))
hypotenuse = ajacent / (math.cos(angle))
print("The length of the hypotenuse is {}".format(hypotenuse))
elif method == "tan" and question == "o":
ajacent = float(input("What is the length of the ajacent?"))
opposite = ajacent * (math.tan(angle))
print("The length of the opposte is {}".format(opposite))
elif method == "tan" and question == "a":
opposite = float(input("What is the length z"))
get_method(question, method)
main(question, method)
正如您可能看到的,我在将变量传递到函数中时遇到了一个问题。是的,我知道我的代码效率很低,但我只是一个新手,所以让我放松一下既然GETX方法似乎是个问题,让我们来看一看。首先,您传入了忽略并重置的参数,因此将这些参数抛给这个函数-定义并调用它,而不使用任何参数
我添加了两个助手函数来简化get_方法代码:
现在,您可以通过以下方式进行呼叫:
method, question = get_method()
如果您遇到异常,请编辑问题以包含完整的异常回溯。不过,有时会在错误的行上报告SyntaxError,因为错误的原因是Python对代码结构的混淆,而这些混淆通常是由于缺少右括号之类的原因造成的。
method, question = get_method()