[Tutor] <TUTOR>My first (er...working...) script ever

Gerrit Holl gerrit@nl.linux.org
Sat Feb 22 16:19:01 2003


Predrag Ivanovic schreef op zaterdag 22 februari om 22:04:30 +0000:
> 
> Hello...
> 
> Programming and Python are pretty much new to me,but it's been fun so far...

I can guarantee that it will stay like that!

>     print " 'o'  za opcije"
>     print " 'k'  za obim kruga "
>     print " 'p'  za obim pravougaonika "
>     print " 'c'  za obim kvadrata"
>     print " 'e'  za  exit"

I'm afraid I cannot comment on this one, as I do not understand
a single word ;)

> ## circle area 
> def krug(radius) :
>     return  (math.pi*radius)

Well, I think the area of a circle is math.pi*(radius**2), but that's not
a Python issue. The circumference is math.pi*radius*2 (note the differenc
between ** (pow) and * (mul)).


> ##area of rectangle 
> def pravougaonik(visina,duzina) :
>     return ((visina*2)+(duzina*2))

This is actually the circumference, not the area. The area
is width*height (visina*duzina?)

> def kvadrat(stranica) :
>     return (stranica*4)

Again, this is the circumference.

> choice="o" ##initial value
> 
> while choice != "e" :
>     if  choice == "k" :
>         r=input("poluprecnik kruga :  ")
>         print "obim je : ", krug(r)

For error checking, it is recommended to use raw_input.
If a user enters "100**100**100**100", it is going to take
a long time to finish. You can betted to r=raw_input(...)
and then try to use int(). That is more error proof.

>     elif choice == "p" :
>         v=input ("visina  : ")
>         h=input (" duzina :")
>         print "obim : ", pravougaonik(v,h)

Same here...

>     elif choice == "c" :
>         d=input ("stranica :") 
>         print "obim je : ", kvadrat(d)

and here.

>     elif choice =="e":
>         sys.exit()

>     elif choice != "e":
>         print_options()
>     choice = raw_input("opcija : ")

But further, this program looks OK.

You can use the cmd module if you want to extend your program:

http://www.python.org/dev/doc/devel/lib/module-cmd.html
"The Cmd class provides a simple framework for writing line-oriented command interpreters."

But that may very well be overkill for your exercise program.

yours,
Gerrit.

-- 
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/