-3

write a program that prompts the user for the radius and height of a 3-dimensional cone and then calculates and prints the surface area and volume of the cone. The calculation of the surface area and the volume will be done in functions, as will the gathering of the inputs.

Your program for this part will function as follows:

  1. Print out a message indicating what the program does.
  2. Prompt the user for the radius (a non-negative float) in feet.
  3. Prompt the user for the height (a non-negative float) in feet.
  4. Print the radius and height, but rounded to 2 decimal digits.
  5. Print the surface area and volume, rounded to 2 decimal digits.

Here is what I done so far:

import math

print("This Program will calculate the surface area and volume of a cone."
  "\nPlease follow the directions.")
print()
print()
r = input(str("What is the radius in feet? (no negatives): "))
h = input(str("What is the height in feet? (no negatives): "))

math.pi = (22.0/7.0)
math.sqrt()
surfacearea = int(math.pi*r**2)+int(r*math.pi(math.sqrt(r**2+h**2)))
print("The surface area is", surfacearea)
print()
volume = (1/3)*math.pi*r**2*h
print ("The volume is", volume)

print()
print("Your Answer is:")
print()

print("A cone with radius", r, "\nand hieght", h,"\nhas a volume of : ",volume,
  "\nand surface area of", surfacearea,)

I keep getting errors

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

TypeError: can't multiply sequence by non-int of type 'float'

Can anyone help me get pass this little wall block I think 'float' is part of the problem. I think the setup is good but the execuption is the problem.

Community
  • 1
  • 1
Thomas Jones
  • 355
  • 2
  • 11
  • 17
  • Please don't just show the error, show the traceback, so we can see which line caused each error, and don't have to try to guess. – abarnert Feb 19 '13 at 22:57
  • Also, this can't be your real code, or you'd get a `TypeError` from that `math.sqrt()` line before you even get to the one for `**`. If you want us to debug your code, you have to give us the same code you give to Python. – abarnert Feb 19 '13 at 23:00

1 Answers1

2

I'm assuming you're using Python 3, so input is just returning a string.

So:

r = input(str("What is the radius in feet? (no negatives): "))
# ...
surfacearea = int(math.pi*r**2) #+ ...

That will raise this error because you're trying to square a string. You can't do that.

If you add r = float(r) after the input, then it will give you a float (which you can square), or raise an exception if the user types in something bad.

Meanwhile, what is the str for in that line? What type do you think "What is the radius in feet? (no negatives): " is? Are you trying to accomplish something, or did you just insert it without even knowing why?

Likewise, in this line:

surfacearea = int(math.pi*r**2)+int(r*math.pi(math.sqrt(r**2+h**2)))

Why are you converting the floating point values to int? The assignment says the values should be "rounded to 2 digits".

More generally, if you get an error on some line of code and have no clue why, try breaking it up. There's an awful lot happening in that one line. Why not try this:

r_squared = r**2
pi_r_squared = math.path * r_squared
int_pi_r_squared = int(pi_r_squared)
h_squared = h**2
r_squared_h_squared = r_squared + h_squared
sqrt_r2_h2 = math.sqrt(r_squared_h_squared)
# etc.

Then you can see which one is not working, and figure out why, without having to look at a big mess of code and guess. You can even debug it by adding pdb breakpoints or print calls at a particular line, to make sure that each value is what you think it should be.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thanks abarnet. I'm new to python and it is touch and go. But I learn everyday the more I try. For a noob like me messes is what comes with learning. Thanks for your guidance and time. – Thomas Jones Feb 19 '13 at 23:29