Thread: radius

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

    Join Date
    Feb 2004
    Posts
    2
    Rep 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?
  2. #2
  3. 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)
  4. #3
  5. 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)
  6. #4
  7. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    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.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    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.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    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

Similar Threads

  1. Zip code radius?
    By peanutty in forum ColdFusion Development
    Replies: 8
    Last Post: November 16th, 2012, 08:09 AM
  2. Moving things with the keyboard
    By FalconixB in forum Java Help
    Replies: 13
    Last Post: October 1st, 2003, 05:01 PM
  3. Does anyone use VOP Radius?
    By Stink Sleeve in forum Dev Shed Lounge
    Replies: 0
    Last Post: June 20th, 2003, 04:24 PM
  4. distance by radius (sorting arrays)
    By Fataqui in forum PHP Development
    Replies: 1
    Last Post: May 25th, 2003, 12:28 PM

IMN logo majestic logo threadwatch logo seochat tools logo