By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,520 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,520 IT Pros & Developers. It's quick & easy.

building a small calculator

P: n/a
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.

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 20 Apr 2005 05:18:17 -0700, al*****************@gmail.com
<al*****************@gmail.com> wrote:
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!

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #2

P: n/a
I get the following error message when using the command sys.exit().

name 'sys' is not defined.

Jul 19 '05 #3

P: n/a
On 20 Apr 2005 05:31:15 -0700, al*****************@gmail.com
<al*****************@gmail.com> wrote:
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>.

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #4

P: n/a
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.

Jul 19 '05 #5

P: n/a
On 20 Apr 2005 05:41:37 -0700, al*****************@gmail.com
<al*****************@gmail.com> wrote:
Sorry. This is my first day using Python.
Nothing to apologise for. We were all new to Python once.
Is there an equivelent to the goto command? I want to find some way to
take the user back to the main menu.


Funny you should mention that...

http://groups-beta.google.com/group/...cca2f994881220

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #6

P: n/a
al*****************@gmail.com 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
Jul 19 '05 #7

P: n/a
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.

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.