2 Replies - 11864 Views - Last Post: 31 August 2009 - 07:52 PM Rate Topic: -----

#1 joliejoe13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-August 09

Calculate volume and surface area of a sphere given its radius as inpu

Post icon  Posted 31 August 2009 - 10:10 AM

import math
def main():
v=math.pi*(4/3)*(r**3)
a=math.pi*4*(r**2)
return
radius=float(raw_input("Radius:"))
print "Solution of volume:",volume(radius),"and solution of area",area(radius)

[b]The second part is the error that is generated and i am not able to understand it and how to solve it...






IDLE 1.2 ==== No Subprocess ====
>>>
Radius:"7"
Traceback (most recent call last):
File "C:\Documents and Settings\user\My Documents\Lecture Notes\Programming Methodology\Labsheet Programming Methodology\Areasphere&volumeW2Q2.py", line 10, in <module>
radius=float(raw_input("Radius:"))
ValueError: invalid literal for float(): "7"
>>>

Is This A Good Question/Topic? 0
  • +

Replies To: Calculate volume and surface area of a sphere given its radius as inpu

#2 code_m  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 194
  • Joined: 21-April 09

Re: Calculate volume and surface area of a sphere given its radius as inpu

Posted 31 August 2009 - 10:15 AM

Where did you define the variable "r" ???
Was This Post Helpful? 0
  • +
  • -

#3 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 161
  • View blog
  • Posts: 247
  • Joined: 19-July 09

Re: Calculate volume and surface area of a sphere given its radius as inpu

Posted 31 August 2009 - 07:52 PM

There are several problems here:
1. Please, when you post code use the code /code tags. Otherwise the identitation is omitted.
2. Your Error Message: When the program prompted you to input a radius you typed in "7" or '7' instead of just 7. The string "7" contains characters, that are not numbers or a decimal point ( the qoutation marks). So python cannot convert that to a float.
3. It seams you haven't understood how functions work. A function looks like the following. Am argument is passed to the function, some computation is done with it and a result is returned. Your code doesn not look like that!
def double_up(value):
	result = value * 2
	return result

val = 4

#this will print 8
print double_up(val)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1