[Tutor] how to rewrite area.py

Alan Gauld alan.gauld at btinternet.com
Thu Feb 23 01:46:41 CET 2012


On 22/02/12 23:45, William Stewart wrote:
> so I copied your format except I changed shape 3 to circle, Did I do the
> circle part right?

Nearly.
The circumference is wrong, you need to calculate it not read it from 
the user.


> and would this be considered separate functions?

No, but your second variant is separate functions. You just need to 
insert them into the first version as appropriate. (After you get them 
working of course!:-)

> *elif shape == 3:
>         area = float(input("Radius: "))
>         circumference = float(input("radius: "))
>         print( "Area of Circle = ", pi*radius**2 )
> shape = int(input(menu))*
> while True:
> selection = raw_input("Please select an option from the menu.: ")
>
> def get_area_of_square():
> print "Please enter the width of a square"
This line needs to be an input as you did above.
All you are doing here is printing a message not reading anything back.
And certainly not assigning anything to width.

> area = width**2
> return area

That defines the function but does not call it. You should put the 
definition outside the loop - above all the code above - and then call 
it inside the if/else statement:

area = get_area_of_square()

or even

print "Area of square = ", get_area_of_square()

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list