[Tutor] calculator

Joerg Woelke lumbricus@gmx.net
Tue, 28 Aug 2001 11:49:37 +0200


On Tue, Aug 28, 2001 at 04:58:03AM -0400, melvin2001 wrote:
> well......im 15 years old and i just started messing with python a few days ago so obviously im not gonna be very good :op but i wrote a little program that does basic calculations and its not done yet i add to it just about every day. just wanted to show you guys to see what you think maybe help me out a little too.
> 
> test = 1
> print "alright now for some real math"
> print
> while test == 1:

Am i missing something or is this equivalent to "while 1:"
where are the breaks?

>     print "please choose an option"
>     print
>     print "1. circle area"
>     print "2. square area"
>     print "3. rectangle area"
>     print "4. square root"
>     print "5. add numbers"
>     shape = input("> ")

How about error checking?
try:
	shape=int(raw_input("> "))
	# because users like to enter nonesense ;-)
except ValueError:
	print "Need number"
	...
	
>     if shape == 1:
>         print "would you like to input radius or diameter?"
>         print "1. radius"
>         print "2. diameter"
>         question = input ("> ")
>         if question == 1:
>             print "what is the radius?"
>             radius = input("> ")
>             radius = 3.14*radius**2
>             print radius
>         elif question == 2:
>             print "what is the diameter?"
>             diameter = input ("> ")
>             diameter = diameter/2
>             diameter = diameter**2 * 3.14

You want to pack thing like that in functions.

>             print diameter
>         else:
>             print
>             print "whoops thats not good"
>             print
>     elif shape == 2:
>         print "what is the length of a side?"
>         side = input("> ")
>         side = side*4
>         print side
>     elif shape == 3:
>         print "what is the length?"
>         length = input("> ")
>         print "what is the height?"
>         height = input("> ")
>         height = height*length
>         print height
>     elif shape == 4:
>  lauren = 0
>  print "what number do you need the square root for?"
>  number = input("> ")
>  guess = 1.0
>  while lauren != guess:
>   lauren = (number / guess + guess)/2
>   guess = (number / lauren + lauren)/2
>  print guess
>     elif shape == 5:
>         krysten = input("first number to add> ")
>         add = input("second number to add> ")
>         equals = add + krysten
>         print "is that all?"
>         print "1. yes"
>         print "2. no"
>         all = input("> ")
>  if all == 1:
>             print equals
>         if all == 2:
>             while all == 2:
>                 third = input("next number to add> ")
>                 equals = equals + third
>                 print "is that all?"
>           print "1. yes"
>           print "2. no"
>           all = input("> ")
>         print equals
>     else:
>         print
>         print "whoops thats not good"
>         print

Good luck!
HTH, HAND
J"o! :-)

-- 
Q:	What do Winnie the Pooh and John the Baptist have in common?
A:	The same middle name.