### Thread: radius

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2004
Posts
2
Rep Power
0

#### radius

Hi Folks,

Trying to write a simple python script to calculate radius.

This works:

def area_circ(radius):
return radius*radius*3.14

r = input('radius? ')
print 'Area =',area_circ(r)

But this doesn't:

def area_circ(radius):
return radius*radius*3.14

r = input('radius: ')
while r <= 0:
r = input('radius? ')
print 'Area =',area_circ(r)

What am I doing wrong?
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2004
Posts
2
Rep Power
0
Sorry...I didn't indent properly in my post, but in my code I have indented properly. I'll try posting again.

def area_circ(radius):
return radius*radius*3.14

r = input('radius: ')
while r <= 0:
r = input('radius? ')
print 'Area =',area_circ(r)
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
217
Rep Power
0
You need to indent the lines after your function declaration (although I'm not sure if that was a typo or not).

This seems to work fine for me:

Code:
```def area_circ(radius):
return radius * radius * 3.14

r = input('radius: ')

while r <= 0:
r = input('radius: ')

print 'Area = ', area_circ(r)```
4. If you're looking for precision, too, just import math and use math.pi instead of 3.14.
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
133
Rep Power
15
Well, as it stands, you're saying: While r is less than or equal to 0, go into the while loop.

What you probably want is to say: While r is more than or equal to 0, go into the while loop, ie.: "while r >= 0:"

Code:
```def area_circ(radius):
return radius*radius*3.14

r = input('radius: ')
while r >= 0:
r = input('radius? ')
print 'Area =',area_circ(r)```
Last edited by percivall; February 15th, 2004 at 02:41 AM.
6. Perc: in you're example or you get asked for r twice and with no reason. And no <= should work fine.

If you want to use >= then you're best bet would be to remove the first raw_input() call (outside the loop) and give it the value of '0' so we jump right into the loop.

Mark.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
133
Rep Power
15
Originally Posted by netytan
Perc: in you're example or you get asked for r twice and with no reason. And no <= should work fine.
First of all: I just copied the example.
Secondly: Yes, <= works beatifully if your radius is less than or equal to zero. This is of course in violation of every physical law, but of course it works well in the world of computers. I can't really understand why you'd want a program to work that way though.

The normal way to write a simple app like this is to say that as long as the radius is within the limits of physical law, that is to say >= 0, the loop should continue. That's why I recommended the use of >=.
Last edited by percivall; February 15th, 2004 at 09:25 AM.
8. Naw you missunderstood me perc, oh and sorry if that came off bitchy i'd just woken up.

Anyway all i was trying to explain was that if you want the program to you use >= then you should set the original value of 'r' to 0 instead of asking for it and jumping into the loop i.e.

Code:
```def area_circ(radius):
return radius*radius*3.14

r = 0

while r >= 0:
r = input('radius? ')

print 'Area =', area_circ(r)```
What this actually says is ask for input while the area is >= 0. So the loop will continue untill a negative number is suplied. Which doesn't seem right to me assuming you only want to run the function check once (when a valid value is entered) like in nb and Xx's example.

On the other hand if we do want to run the funcion on each value no matter if the number is negative or possative (with the loop exiting when a negativ number is supplied) then you're right it works perfectly!!

Code:
```...
r = 0

while r >= 0:
r = input('radius? ')

print 'Area =', area_circ(r)```
*breaths*, hope you got that

Mark.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
133
Rep Power
15
Yeah. I got you this time. I figured that nbhiker wanted the app to loop so that he could enter multiple radii; otherwise it seemed strange that he would complain it didn't work. Anyway: If I just wanted a function that required a correct number I'd probably write it like this (as another perspective for nbhiker):
Code:
```from math import pi
def area_circ(radius):
if radius < 0:
raise ValueError, "Radius must be a positive number."
return radius*radius*pi```