【问题标题】:Confused about Functions对函数感到困惑
【发布时间】:2021-12-31 05:55:48
【问题描述】:

所以我目前正在为我的 python 编码类做作业,我必须得到一个圆柱体的面积,我应该使用函数,所以它看起来更干净,但我不太确定如何准确从头开始做作业,我看过很多视频,但似乎无法真正理解函数,我的代码目前看起来像这样,但是每当我运行我的代码时,我都无法获得“def calc():”部分要运行,请问我可以指点一下吗?

def info():
    r = float(input("What is the radius of the cylinder? "))
    h = float(input("What is the height of the cylinder? "))
    print("The cylinders area is", area)

def calc():
    pi = 22/7
    area = ((2*pi*r) * h) + ((pi*r**2)*2)
    return pi, area

info()

【问题讨论】:

  • 嗨,info() 中的最后一行应该是print("The cylinder's area is ", str(calc(r, h)))。您还应该将def calc(): 更改为def calc(r, h):注意转换为 str! 而且您不应该返回 pi。只返回area。这是你唯一需要的。

标签: python area


【解决方案1】:

不需要那么多功能。

def info():
    r = float(input("What is the radius of the cylinder? "))
    h = float(input("What is the height of the cylinder? "))
    calc(r,h)
    

def calc(r,h):
    pi = 22/7
    area = ((2*pi*r) * h) + ((pi*r**2)*2)
    print("The cylinders area is", area)
    

info()

【讨论】:

  • 是的,我认为我没有这样做,但是我的老师希望我们练习使用它们!
  • LastCheck ,重新审视这个,我认为这就是你所需要的。
  • 感谢大侠的帮助,不胜感激!
【解决方案2】:

在这种情况下,我将半径设置为 3,高度设置为 6。您需要定义变量。 然后它应该工作。在这种情况下,我使用 numpy 导入精确的 pi 变量。

【讨论】:

    【解决方案3】:

    您必须将calc() 函数放在info() 函数之上。您必须实际调用将返回圆柱面积的calc() 函数。我提供了下面的代码,应该可以解决您的问题。希望你看懂代码!

    def calc(r, h): # Calculates the area of cylinder
        pi = 22/7
        area = ((2*pi*r) * h) + ((pi*r**2)*2)
        return area
    
    def info(): # Takes the radius and height from user.
           r = float(input("What is the radius of the cylinder? "))
           h = float(input("What is the height of the cylinder? "))
           
           return f"The area is {calc(r, h)}"
     
    # r = 5
    # h = 10   
    print(info())
    
    # using parameters in functions instead of inputs:
    
    def info2(r, h):
        return f"The area is {calc(r, h)}"
    
    r = 5
    h = 10 
    print(info2(r, h))
    
    # Comparing areas of cylinders
    
    area1 = calc(5, 10)
    area2 = calc(10, 15)
    
    if area1>area2: #Area1 is greater than Area2
        print("Area1 is greater than Area2")
    elif area2>area1: #Area2 is greater than Area1
        print("Area2 is greater than Area1")
    else: #Both are equal
        print("Both are equal")
    

    【讨论】:

    • 非常感谢!如果您不介意帮助我,我还有一个问题,我正在尝试比较两个气缸,看看哪个气缸更大,我该怎么做?
    • @LastCheck 定义另一个函数,将两个圆柱体的面积作为参数/参数,比较面积并返回更大的面积。
    • 对不起,我不太明白你的意思。
    • @LastCheck 我已经编辑了比较两个圆柱体面积的答案
    • 非常感谢您的帮助,我非常感谢您为帮助我成长所做的工作!
    【解决方案4】:

    您在评论中提到您想要输入多个圆柱体并确定哪个是较大的 - 我认为您希望让输入圆柱体的函数返回圆柱体本身,以便可以使用音量函数。

    from dataclasses import dataclass
    from math import pi
    
    
    @dataclass
    class Cylinder:
        radius: float
        height: float
        name: str
    
    
    def volume(c: Cylinder) -> float:
        return (2*pi*c.radius) * c.height + (pi*c.radius**2)*2
    
    
    def input_cylinder(name: str) -> Cylinder:
        r = float(input(f"What is the radius of the {name} cylinder? "))
        h = float(input(f"What is the height of the {name} cylinder? "))
        return Cylinder(r, h, name)
    
    
    if __name__ == '__main__':
        cylinders = [input_cylinder(name) for name in ('first', 'second')]
        for c in cylinders:
            print(f"The {c.name} cylinder's volume is {volume(c)}")
        print(f"The bigger cylinder is the {max(cylinders, key=volume).name} one.")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多