使用直径输入/Def 函数查找圆的面积 [已关闭]

finding the area of a circle using diameter input / Def functions [closed]

提问人:AJHedges 提问时间:9/28/2023 最后编辑:S3DEVAJHedges 更新时间:9/28/2023 访问量:58

问:


这个问题是由一个错别字或一个无法再重现的问题引起的。虽然类似的问题可能在这里成为主题,但这个问题的解决方式不太可能帮助未来的读者。

上个月关闭。

我正在尝试使用直径输入找到以平方为单位的圆的面积。我觉得我离正确不远了。程序打开,但在排除输入后关闭。这是我到目前为止的代码 这是我到目前为止的代码..

def circleArea(diameter_of_the_circle):
    radius = diameter / 2
    pi = 3.14159
    result = pi * radius * radius
    return(result)

dt = float(input("\nEnter the diameter of the circle in cms: "))

anwser = circleArea(diameter_of_the_circle)

print("\nThe Area of the circle is:", anwser)

input("\nPress Enter to exit")
Python 函数

评论

2赞 user19077881 9/28/2023
使用 anwser = circleArea(dt)
2赞 Barmar 9/28/2023
这也行得通。您只需要在变量名称中保持一致即可。如果从输入中分配变量,请在函数调用中使用该变量。
1赞 Barmar 9/28/2023
您应该从您的版本中得到一个错误,告诉您未定义。diameter_of_the_circle
3赞 Barmar 9/28/2023
在终端窗口中运行脚本,而不是为其弹出一个新窗口,因为后一种方法会自动关闭窗口,并且您永远不会看到错误。
2赞 Barmar 9/28/2023
你不应该使用 Python 2.x,它是几年前的 EOL,除非你需要它来编写遗留代码。

答:

0赞 Amir 9/28/2023 #1

您的代码存在几个问题:

  1. 在函数定义中,参数是 ,然后你有 .什么?diameter_of_the_circleradius = diameter / 2diameter
  2. 您定义了函数输入,但随后将其用作函数输入。dtdiameter_of_the_circle
1赞 Libnist 9/28/2023 #2

您忘记使用与输入参数相同的名称。

import math

def circleArea(diameter):
    radius = diameter / 2
    result = math.pi * radius * radius
    return(result)

dt = float(input("\nEnter the diameter of the circle in cms: "))

anwser = circleArea(dt)

print("\nThe Area of the circle is:", anwser)

input("\nPress Enter to exit")