简体   繁体   中英

Confused about Functions

So I'm currently doing an assignment for my python coding class and I have to get the area of a cylinder and I'm supposed to be using functions so it looks cleaner, but I'm not so sure how to exactly do my assignment from scratch, I've watched a lot of videos but can't really seem to understand functions, my code looks like this currently, but whenever I run my code I can't get the "def calc():" part to run, could I get some pointers please?

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

Don't need so many funcatin.

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

in this case I put the radius as 3 and height as 6. You need to define you variables. Then it should work. in this case I used numpy to import an exact pi variable.

You have to put the calc() function above the info() function. You have to actually call the calc() function which will return the area of cylinder. I have provided the code below which should solve your problem. Hope you understand the code!

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

You mentioned in a comment that you wanted to input multiple cylinders and determine which was the larger one -- I think for that you want to have your function that inputs a cylinder return the cylinder itself so that it can be compared using the volume function.

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.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM