0

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()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
John Dias
  • 29
  • 1
  • 2
  • 9
  • Some of the code looks like it's written for Python 3; are you actually using Python 2.7? In Python 2.x you'd probably be getting a `TypeError` on the `eval()` before you even got to the code where you're getting the error you're asking about. Also, the `print()` call looks like it's Python 3 style. – kindall Nov 30 '14 at 04:27

1 Answers1

2

You need to split out the two values returned by measurement. You have two options:

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

  2. 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.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • He's using Python 3, as evidenced by his `print()` calls, so there's no `raw_input()` – kindall Nov 30 '14 at 04:19
  • @kindall: the OP tagged the question with `python-2.7` (I added the `python` tag). Loads of beginner Python programmers use `print` as if it is a function in Python 2, and it happens to work most of the time. – Martijn Pieters Nov 30 '14 at 04:22
  • If he was using Python 2.x, though, he'd be getting a `TypeError` on the `eval()` of an integer input before even getting to the place where he's getting an error. I think the tag must be incorrect. – kindall Nov 30 '14 at 04:24
  • @kindall or they've been entering numbers with quotes around them all this time. Why not ask the OP by commenting on the question? – Martijn Pieters Nov 30 '14 at 04:25
  • sorry, i am not using python 2.7 there is no raw_input but your first suggestion did work, thank you. – John Dias Dec 01 '14 at 00:36
  • @JohnDias right; your initial tagging suggested that you did; I'll update that part of my answer. – Martijn Pieters Dec 01 '14 at 00:38