这是我的代码使用三角函数来查找三角形上缺少的边。它的工作原理是使用SOHCAHTOA找到这些边。 代码的问题在于,当我尝试运行代码时,当我调用用户定义的函数时,它会给我一个无效的语法:" get_method"。 请问您能告诉代码有什么问题,以及如何解决我的程序中出现的这个一致问题。
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)
您可能会看到我将变量传入函数时遇到问题。是的,我知道我的代码效率很低,但我只是新编码,所以让我有些松懈。
答案 0 :(得分:1)
由于get_method()
似乎是问题所在,让我们来看看它。首先,你传递的参数是你忽略并重置的,所以将参数抛给这个函数 - 定义并调用它而不带参数。
我已经为get_method()
代码添加了几个辅助函数:
import sys
def str_input(prompt=None):
return input(prompt).lower().strip()
def boolean_input(prompt=None):
return input(prompt + "Type 'yes' or 'no': ").lower().strip() == "yes"
def get_method():
print("Label the right-angle triangle.")
question = str_input("What are you trying to find? Type 'h' for hypotenuse, 'a' for adjacent or 'o' for opposite: ")
method = ""
if question == "h":
opposite_verify = boolean_input("Do you have the length of the opposite?\n")
if opposite_verify:
method = "sine"
else:
adjacent_verify = boolean_input("Do you have the length of the adjacent?\n")
if adjacent_verify:
method = "cosine"
else:
sys.exit("There's no way to solve this.")
elif question == "a":
hypotenuse_verify = boolean_input("Do you have the length of the hypotenuse?\n")
if hypotenuse_verify:
method = "cosine"
else:
opposite_verify = boolean_input("Do you have the length of the opposite?\n")
if opposite_verify:
method = "tan"
else:
sys.exit("There's no way to solve this.")
elif question == "o":
hypotenuse_verify = boolean_input("Do you have the length of the hypotenuse?\n")
if hypotenuse_verify:
method = "sine"
else:
adjacent_verify = boolean_input("Do you have the length of the adjacent?\n")
if adjacent_verify:
method = "tan"
else:
sys.exit("There's no way to solve this.")
return method, question
现在您可以通过以下方式致电:
method, question = get_method()