I am writing a program that calculates the volume of a cone given the diameter and height but I keep getting this
TypeError: cone() missing 1 required positional argument: 'height'
How do I fix this?
def main():
measure = measurement()
vol = cone(measure)
print("\nThe volume of the cone is,", "%0.2f" % (vol))
def measurement():
diameter = eval(input("Enter the diameter of the cones base:"))
height = eval(input("Enter the height of the cone:"))
return diameter, height
def cone(diameter, height):
pi = 3.14
radius = diameter / 2
volume = (pi * (radius**2) * height) / 3
return volume
main()
You need to split out the two values returned by measurement
. You have two options:
Use tuple assignment and pass the two results to cone
:
diameter, height = measurement()
vol = cone(diameter, height)
Python expects measurement()
to return two values in a sequence now and will assign those two values to diameter
and height
, respectively, before passing those two values separately to cone()
.
Use argument expansion; this asks Python to apply all values in a sequence as separate arguments:
measure = measurement()
vol = cone(*measure)
Note the *
before the measure
argument.
As for your measurement()
function: you don't need to use eval()
; it poses a security risk. Instead, use float()
to interpret user input as real numbers:
def measurement():
diameter = float(input("Enter the diameter of the cones base:"))
height = float(input("Enter the height of the cone:"))
return diameter, height
Also see Asking the user for input until they give a valid response for more advanced user-input techniques.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments