简体   繁体   中英

How to get cylinder volume in python 3

I am newbie in programming. I am just following a course on python which is hosted by Udacity. Here, the code prescribed below does not print the function return.

Would you people please help me😟 Thanks in advance!

def cylinder_volume (height, radius = 5):
pi = 3.14159
return height * pi * radius ** 2

print (cylinder_volume(10, 5))

There were a few things wrong with your program that I've tidied up (I remember starting to learn python as well). You shouldn't set the variable radius to 5 in the function definition, instead input it when calling the function, anyway here's your code tidied up with a few comments that'll hopefully help you out

import math

def cylinder_volume (height, radius):
    pi = math.pi    # more accurate representation of Pi using pythons math library
    volume = height * pi * radius ** 2  # Creates a variable with volume value
    print(volume)   # prints out the volume
    return volume   # returns the volume

cylinder_volume(1, 5)   # using this uses only the print statement in the function

print("-----------------")


print(cylinder_volume(1, 5))    # using this prints the print statement in the function as well as the value returned

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