Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 14th, 2004, 07:27 AM
 nbhiker
Registered User

Join Date: Feb 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0

Hi Folks,

Trying to write a simple python script to calculate radius.

This works:

print 'Area =',area_circ(r)

But this doesn't:

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

What am I doing wrong?

#2
February 14th, 2004, 07:40 AM
 nbhiker
Registered User

Join Date: Feb 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry...I didn't indent properly in my post, but in my code I have indented properly. I'll try posting again.

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

#3
February 14th, 2004, 09:47 AM
 XxChris
Registered User

Join Date: Aug 2003
Posts: 217
Time spent in forums: < 1 sec
Reputation 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:
```

while r <= 0:

print 'Area = ', area_circ(r)
```

#4
February 14th, 2004, 05:47 PM
 SolarBear
onCsdfeu

Join Date: Jul 2003
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 11
If you're looking for precision, too, just import math and use math.pi instead of 3.14.
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz

#5
February 15th, 2004, 03:29 AM
 percivall
Contributing User

Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 11
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):

while r >= 0:
print 'Area =',area_circ(r)```

Last edited by percivall : February 15th, 2004 at 03:41 AM.

#6
February 15th, 2004, 06:42 AM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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.
__________________
programming language development: www.netytan.com Hula

#7
February 15th, 2004, 10:21 AM
 percivall
Contributing User

Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 11
Quote:
 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 10:25 AM.

#8
February 15th, 2004, 01:07 PM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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):

r = 0

while r >= 0:

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:

print 'Area =', area_circ(r)```

*breaths*, hope you got that

Mark.

#9
February 15th, 2004, 06:17 PM
 percivall
Contributing User

Join Date: Jul 2003
Posts: 133
Time spent in forums: < 1 sec
Reputation Power: 11
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
raise ValueError, "Radius must be a positive number."

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > radius