在学校,我们必须编写一个程序来使用毕达哥拉斯理论。我用python 3编写它,但是当我返回cber时程序就结束了。另一方面,bber可以正常工作。有人可以帮忙吗?已经感谢:)
编辑:感谢您的帮助,使用kiezen函数并不能解决所有问题,用户可以选择两个数字,而j和n决定它们在三角形中的线,也就是kiezen函数。这是一个叫做cijfers的函数,我不知道这是否有所不同。我使用return是因为这样,如果他/她输入的内容无效,我可以让用户再次选择数字。我忘了在发布之前删除cber中的ifs。我会尽力 尽快改善我的程序。感谢您的所有反馈:)
def bber():
if (c >= a):
print(str(a) + "^2 + b^2 = " + str(c) + "^2")
print("b^2 = " + str(c) + "^2 - " + str(a) + "^2")
print("b = V(" + str(c**2) + " - " + str(a**2) + ")")
print("b = V" + str((c**2) - (a**2)) + " = " + str(math.sqrt((c**2) - (a**2))))
if (a >= c):
print("De rechthoekzijde kan niet langer zijn dan de schuine zijde.")
cijfers()
def cber():
if (a >= b):
print(str(a) + "^2 + " + str(b) + "^2 = c^2")
print("c^2 = " + str(a) + "^2 + " + str(b) + "^2")
print("c = V(" + str(a**2) + " + " + str(b**2) + ")")
print("c = V" + str((a**2) + (b**2)) + " = " + str(math.sqrt((a**2) + (b**2))))
if (b >= a):
print(str(a) + "^2 + " + str(b) + "^2 = c^2")
print("c^2 = " + str(a) + "^2 + " + str(b) + "^2")
print("c = V(" + str(a**2) + " + " + str(b**2) + ")")
print("c = V" + str((a**2) + (b**2)) + " = " + str(math.sqrt((a**2) + (b**2))))
def kiezen():
x = int(input("Wat is de lengte van de eerste zijde?"))
xz = input("Is deze zijde een rechthoekzijde (J/N)?")
print(" ")
y = int(input("Wat is de lengte van de tweede zijde?"))
yz = input("Is deze zijde een schuine zijde (J/N)?")
print(" ")
return kiezen()
if xz == "j" or "J":
if yz == "n" or "N":
b = y
a = x
return cber()
if yz == "j" or "J":
c = y
a = x
return bber()
答案 0 :(得分:0)
发生了一些问题。
在您的代码中,您使用的是math.sqrt
,因此第一行需要在文件的开头实际导入math
模块:
import math
您无法访问函数中的变量。要将它们传递给函数,必须将它们指定为函数参数:
def bber(a,c):
从积极的方面来看,函数bber
在语句if (c >= a)
中报告正确的答案。但是,以下条件语句if (a >= c)
调用了函数cijfers()
,该函数实际上并不存在。在这种情况下,每次 a 大于或等于 c 时,程序将打印NameError
。
cber
可以工作,但是实际上不需要if
语句,因为无论 b ,您都可以获得变量 c 。 strong>大于 a ,或者 a 大于 b 。不过,您可能要考虑检查其他类型的输入(例如文本,负数,浮点数等)。在这里,您可以简化cber
函数,还必须传递实际参数:
def cber(a, b):
print(str(a) + "^2 + " + str(b) + "^2 = c^2")
print("c^2 = " + str(a) + "^2 + " + str(b) + "^2")
print("c = V(" + str(a**2) + " + " + str(b**2) + ")")
print("c = V" + str((a**2) + (b**2)) + " = " + str(math.sqrt((a**2) + (b**2))))
函数kiezen
实际上并未在您的代码中执行任何操作。它是已定义的,但是您显然不会在任何地方使用它。
在函数内部定义的变量是该函数的局部变量,而在函数外部定义的变量(即无缩进)是全局变量。当需要在函数内部使用全局变量时,必须将其作为函数参数传递。有关此主题的更多信息,您可以阅读有关“范围”,“全局范围”和“本地范围”的概念。您还可以在官方Python文档here中找到示例。
现在,您可以了解如何使用全局定义的变量,我将使用您不完整的kiezen
函数而不实际将其用作函数,因此可以直接执行代码在您的程序中。
return
,因为这是为了:返回函数的结果。这意味着您必须通过删除 return 关键字来更改代码return cber()
和return bber()
。
在用input
提问时,您缺少空格。键入答案时,答案将出现在字符串中最后一个字符的旁边。
当您要检查多个选项时(与XZ == "j" or "J"
一样),可以改用列表和关键字 in 。
最后需要进行一些修改才能使您的程序正常工作。注释出现在带有#符号的行上。
# To use a function provided by the math module, you have to import it first
import math
# You have to get "a" and "c" from somewhere, so you pass them as parameters
def bber(a, c):
if (c >= a):
print(str(a) + "^2 + b^2 = " + str(c) + "^2")
print("b^2 = " + str(c) + "^2 - " + str(a) + "^2")
print("b = V(" + str(c**2) + " - " + str(a**2) + ")")
print("b = V" + str((c**2) - (a**2)) + " = " + str(math.sqrt((c**2) - (a**2))))
if (a >= c):
print("De rechthoekzijde kan niet langer zijn dan de schuine zijde.")
# Same scenario here: "a" and "b" must be defined somehow
# Note that the "if" statements were unnecessary
def cber(a, b):
print(str(a) + "^2 + " + str(b) + "^2 = c^2")
print("c^2 = " + str(a) + "^2 + " + str(b) + "^2")
print("c = V(" + str(a**2) + " + " + str(b**2) + ")")
print("c = V" + str((a**2) + (b**2)) + " = " + str(math.sqrt((a**2) + (b**2))))
# Note that a space has been added at the end of each string
# where you use "input".
X = int(input("Wat is de lengte van de eerste zijde? "))
XZ = input("Is deze zijde een rechthoekzijde (J/N)? ")
print(" ")
Y = int(input("Wat is de lengte van de tweede zijde? "))
YZ = input("Is deze zijde een schuine zijde (J/N)? ")
print(" ")
if XZ in ["j", "J"]:
if YZ in ["n", "N"]:
b = Y
a = X
# You have to remove the word "return", this is not a function
cber(a, b)
if YZ in ["j", "J"]:
c = Y
a = X
# You have to remove the word "return", this is not a function
bber(a, c)
正如我之前提到的,这不是完美的,因为您将需要管理错误。例如,如果您输入文本而不是数字,则无法使用,但这是另一个讨论的主题。
此外,出于本练习的目的,如果您的目标只是打印输出,实际上并不需要“ {” {1}}关键字”,但是请记住,如果需要重用函数的结果,您将必须为其返回一个值。
该概念的简单示例如下:
return