函数在假定的情况下不显示

2024-09-29 23:29:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在学校我们必须写一个程序来运用毕达哥拉斯的理论。我是用python3写的,但是当我返回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()

Tags: 函数程序inputreturnifdefde数字
1条回答
网友
1楼 · 发布于 2024-09-29 23:29:58

有一些问题正在发生。你知道吗

  1. 您需要导入模块。你知道吗

在代码中,您使用的是math.sqrt,因此第一行需要实际导入文件开头的math模块:

import math
  1. 您无法访问函数中的变量。要将它们传递给函数,必须将它们指定为函数参数:

    定义bber(a,c):

从积极的方面来看,函数bber在语句if (c >= a)中报告了正确的答案。但是,下面的条件语句if (a >= c)调用函数cijfers(),而函数实际上并不存在。在这种情况下,每当a大于或等于c时,程序将打印NameError。你知道吗

  1. 函数cber起作用,但实际上不需要有if语句,因为无论b大于aa大于b都可以得到变量c。不过,您可能需要考虑检查其他类型的输入(如文本、负数、浮点等)。你知道吗

下面是如何简化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))))
  1. 函数kiezen实际上没有在代码中执行任何操作。它是有定义的,但显然你不在任何地方使用它。

  2. 函数内部定义的变量是该函数的局部变量,而函数外部定义的变量(即无缩进)是全局变量。当需要在函数中使用全局变量时,必须将其作为函数参数传递。有关此主题的更多信息,您可以阅读“范围”、“全局范围”和“局部范围”的概念。您还可以在官方Python文档here中找到示例。

现在,为了让您了解如何使用全局定义的变量,我将使用不完整的kiezen函数,而不实际将其变为函数,因此代码将直接在程序中执行。你知道吗

  1. 这里的另一个问题是,只能在函数内部使用关键字return,因为这就是它的含义:返回函数的结果。你知道吗

这意味着您必须通过删除return关键字来更改代码return cber()return bber()。你知道吗

  1. 您的问题末尾缺少一个空格input。键入答案时,它将出现在字符串中最后一个字符的旁边。

  2. 当您想要检查多个选项时(就像您对XZ == "j" or "J"所做的那样),您可以改用列表和关键字

下面是一些需要的修改,以使您的程序最终工作。注释出现在带有#符号的行上。你知道吗

# 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)

正如我前面提到的,这并不完美,因为您需要管理错误。例如,如果输入文本而不是数字,它将不起作用,但这是另一个讨论的主题。你知道吗

此外,在本练习中,如果您的目标只是打印输出,则实际上不需要在函数中使用return关键字,但请记住,如果您需要重用函数的结果,则必须为其返回一个值。你知道吗

这一概念的一个简单例子如下:

def my_function():
    return 12

# my_variable will have a value of 12
my_variable = my_function()

相关问题 更多 >

    热门问题