473,382 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

building a small calculator

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
7 2460
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
I get the following error message when using the command sys.exit().

name 'sys' is not defined.

Jul 19 '05 #3
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Dr. Mercury | last post by:
Greetings to all - Someone suggested that Java might be the answer for a little conversion program I need. It's pretty simple, really. Just take a column of numbers: 139 259 433 637
0
by: PatchFactory Support | last post by:
Description: Professional and easy-to-use patch building environment that can help you to create instant patch packages for software and file updating. Generated patch packages are small size...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
3
by: Paul | last post by:
I want to make a simple calculator program but dont know where to get started. This is not GUI but a simple terminal program. It would get input like this Enter number: 5 + 10
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
25
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple...
3
by: itsmichelle | last post by:
This is a very primative code of a java swing calculator. I have assigned all the number buttons and the operator buttons and I can add, subtract, multiply, and divide two numbers together. However,...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.