English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Say i wrote the following program which i didn't:

>>> # area calculations
>>>
>>> print "Welcome to the Area calculations program"
Welcome to the Area calculations program
>>> print "------------"
------------
>>> print

>>> # print out the menu:
>>> print "please select a shape:"
please select a shape:
>>> print "1 rectangle"
1 rectangle
>>> print "2 circle"
2 circle
>>>
>>> # get the users choice:
>>> shape = input("1 or 2")
1 or 21
>>>
>>> # 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
else:
radius = input("please enter the radius:")
area = 3.14*(radius**2)
print "The area is", area



How would i actually be able to use this program? how do i use it?

2007-11-07 11:26:57 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

Download a python interpreter for your OS.

2007-11-07 11:29:58 · answer #1 · answered by Kasey C 7 · 0 0

The ">>>" in front of each line tells me that you're working in the Python Shell (IDLE). To use this program, the best way is to make a Python (.py) file and run it. Doing this is very simple, and you're more than half way there by already having the program written. Here are the steps to be able to use that program;

1) Open a New Window from the File menu (Ctrl+N).

2) Type your program into the blank window that comes up. Note: you should have two windows open now, the Python Shell (IDLE) and the new blank window. You should now be working in the blank window.

When typing your program in, omit the ">>>" as this is just IDLE's prompt for you to enter in an expression. To get you started, here are the first few lines of your program...

# area calculations
print "Welcome to the Area calculations program"
print "------------"
print
# print out the menu:
print "please select a shape:"

Note that the output for each line is omitted also. For example, when you say print "----" there is no ---- on the next line as in your original code.

3) Save the file. Give it a generic name and save it somewhere accessible.

4) Run the program. This can be done by going to the Run menu and selecting Run Module, or simply pressing F5. If you did not do step 3, it will now force you to.

Control will automatically be given back to IDLE and all of the output from your program will be displayed and all inputs will be prompted (to get shape, height, width and radius).


One other helpful thing is the use of functions. The transition from your code to a function is simple. Follow this template:

# comments
def functionName():
print "An introduction"
usrInput = input("Enter a value: ")
print "Your input was",usrInput

functionName()


The most common function name to use is "main". So the respective lines would now be "def main()" and "main()". The latter is a function call for IDLE to run the main function. "def main()" tells IDLE that you're DEFining what the main function actually is.

2007-11-11 09:14:55 · answer #2 · answered by Ricky V 2 · 2 0

fedest.com, questions and answers