Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old February 14th, 2004, 06:27 AM
nbhiker nbhiker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 nbhiker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question 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?

Reply With Quote
  #2  
Old February 14th, 2004, 06:40 AM
nbhiker nbhiker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 nbhiker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

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

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

Reply With Quote
  #3  
Old February 14th, 2004, 08:47 AM
XxChris XxChris is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 217 XxChris User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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:

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

r = input('radius: ')

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

print 'Area = ', area_circ(r)

Reply With Quote
  #4  
Old February 14th, 2004, 04:47 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
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

Reply With Quote
  #5  
Old February 15th, 2004, 02:29 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #6  
Old February 15th, 2004, 05:42 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #7  
Old February 15th, 2004, 09:21 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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 09:25 AM.

Reply With Quote
  #8  
Old February 15th, 2004, 12:07 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old February 15th, 2004, 05:17 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > radius


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway