School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,127 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,052 people online right now. Registration is fast and FREE... Join Now!




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

 

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

joliejoe13

31 Aug, 2009 - 09:10 AM
Post #1

New D.I.C Head
*

Joined: 28 Aug, 2009
Posts: 1








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"
>>>

User is offlineProfile CardPM
+Quote Post


code_m

RE: Calculate Volume And Surface Area Of A Sphere Given Its Radius As Inpu

31 Aug, 2009 - 09:15 AM
Post #2

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 124



Thanked: 8 times
My Contributions
Where did you define the variable "r" ???
User is offlineProfile CardPM
+Quote Post

Nallo

RE: Calculate Volume And Surface Area Of A Sphere Given Its Radius As Inpu

31 Aug, 2009 - 06:52 PM
Post #3

New D.I.C Head
*

Joined: 19 Jul, 2009
Posts: 24



Thanked: 3 times
My Contributions
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!
CODE
def double_up(value):
    result = value * 2
    return result

val = 4

#this will print 8
print double_up(val)

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:26PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month