building a small calculator

  • Thread starter aleksander.helgaker
  • Start date
A

aleksander.helgaker

I'm learning to program python on my Mac and I'd like some help. I
followed a tutorial which showed how to make a calculator for working
out the area of a shape. E.g. the area of a circal is pi*r*r.

To practice a bit I thought I'd make the program more advanced but I'm
already having difficoulties. Here is what I have so far.

<code>
# Area calculation program
print "Welcome to the Area calculation program"
print "---------------------------------------"
print

# Print out the menu:
print "Please select a shape:"
print "1 Rectangle"
print "2 Circle"
print "3 Square"

# Get the user's choice:
shape = input("> ")

# Calculate the area:
if shape == 1:
height = input("Please enter the height: ")
width = input("Please enter the width: ")
area = height*width
print "The area is", area
elif shape == 2:
radius = input("Please enter the radius: ")
area = 3.14*(radius**2)
print "The area is", area
else:
length = input("Please input the length: ")
area = length**2
print "The area is", area
</code>

Now once the user has select a shape and typed in the lengths required,
the result is printed and the program quits. What I want to do is make
the program display the result and then make it wait for the user to
press return (or something like that) after which the screen will be
cleared and they'll be back at the main menu.

Also I would like to add option nr 4 to the menu and make that the quit
command, but what command does python have to stop a program? I tried
end but that did not work.
 
S

Simon Brunning

Now once the user has select a shape and typed in the lengths required,
the result is printed and the program quits. What I want to do is make
the program display the result and then make it wait for the user to
press return (or something like that) after which the screen will be
cleared and they'll be back at the main menu.

raw_input('Hit Enter to continue...')
Also I would like to add option nr 4 to the menu and make that the quit
command, but what command does python have to stop a program? I tried
end but that did not work.

sys.exit()

Welcome to Python!
 
A

aleksander.helgaker

I get the following error message when using the command sys.exit().

name 'sys' is not defined.
 
S

Simon Brunning

I get the following error message when using the command sys.exit().

name 'sys' is not defined.

You really are new at this, aren't you? ;-) You need to import the sys
module before you use it. Put this at the top of your script:

import sys

I'd also suggest that you have a run through the tutorial -
<http://docs.python.org/tut/>. It's time well spent. If you don't get
on with that, there's another couple of tutorials available aimed
directly at those new to programming -
<http://www.livewires.org.uk/python/> and
<http://www.ibiblio.org/obp/thinkCSpy/>.

Lastly, although neophytes are more than welcome here, you might find
the tutor mailing list a good place to ask questions in the early days
of your Python experience. You'll find it here -
<http://mail.python.org/mailman/listinfo/tutor>.
 
A

aleksander.helgaker

Sorry. This is my first day using Python.

Is there an equivelent to the goto command? I want to find some way to
take the user back to the main menu.
 
B

Brian van den Broek

(e-mail address removed) said unto the world upon 2005-04-20 08:41:
Sorry. This is my first day using Python.

Is there an equivelent to the goto command? I want to find some way to
take the user back to the main menu.


When I first started learning Python some long time after having done
some BASIC, I found the lack of goto a puzzle, too. You'll get over it :)

Looking at the code from your original post, you just have a linearly
ordered list of statements, with very little structure. It is much
easier to control program flow in Python if you employ functions.

Keeping in mind that this is a sketch, and I am no expert on Python,
but a hobbyist, try changing it so it is more like this pseudo-code

def print_intro():
# intro display logic here

def prompt_user():
# menu display and input fetching logic here

def rectangle_stuff():
# etc.

def circle_stuff():
#

etc.

then,

def main():
while True:
prompt_user()
# logic to do the right thing with the input goes here
# there are better ways, but as a first go, you could use
# if user_input_value == '1':
# rectangle_stuff()
# etc
if user_input_value == quit_value:
break # or call an exit function to do other stuff on exit

Fill out those functions, and call them in the appropriate order, and
the main function will continue to prompt the user, do what's wanted,
and repeat until the user enters the menu option for quiting. (Once
again, a rough first approximation.)

I'd second the recommendation you received to check out the Tutor list
-- both lists are friendly, but Tutor is perhaps better geared to the
level of explanation that you might find most helpful in these early
stages.

Best,

Brian vdB
 
A

aleksander.helgaker

Thats great info. Thanks.

I guess I would know about this if I read through all the manuals, but
I'm awefull at reading that kind of stuff. I'll just have to plow
through it somehow.
 
S

Steve Holden

Thats great info. Thanks.

I guess I would know about this if I read through all the manuals, but
I'm awefull at reading that kind of stuff. I'll just have to plow
through it somehow.
Take your time. Feel free to ask questions. Simon Brunning recommended
the tutor list - those guys really can help, and it's not like using
that list bars you from posting on comp.lang.python.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top