Small Program Help

Hi all
I'm new to Python and programmning so easy..

I'm struggling with a simple program I wrote to calculate perimeters of squares and rectangles. (yeah I know... but I'm just starting out with practising functions).
Once the user has entered an option and found out their perimeter, I can get the menu showing again, but when the select another option it just quits out of the program. My code is below. If someone could point me in the direction of allowing the user to be able to continue selecting options, right up until the actually press 3 to quit. I've been advised about using loop construct and I've looked on this but I'm still confused about to accomplish my task...anyone have some pointers?

Thank You.

print"------------------------------"
print"1. Calculate perimeter of a Rectangle."
print"2. Calculate perimeter of a Square."
print"3. Quit."
print

def perrec(x,y):
return x+y+x+y

#Function 2 is the perimeter of square
def persquare(x):
return x*4

if number==1:
x=input("Please enter width of Rectangle: ")
y=input("Please enter height of Rectangle: ")

elif number==2:
x=input("Please enter width of Square: ")

elif number!=3:
print"This is an invalid option."

else:
print"You have quit."

• : Hi all
: I'm new to Python and programmning so easy..
:
: I'm struggling with a simple program I wrote to calculate perimeters of squares and rectangles. (yeah I know... but I'm just starting out with practising functions).
: Once the user has entered an option and found out their perimeter, I can get the menu showing again, but when the select another option it just quits out of the program. My code is below. If someone could point me in the direction of allowing the user to be able to continue selecting options, right up until the actually press 3 to quit. I've been advised about using loop construct and I've looked on this but I'm still confused about to accomplish my task...anyone have some pointers?

Here's a start in the right direction. What you want is a loop that never ends unless you make it end. What's what "while 1" does. "1" is always "True", so the loop will continue forever unless something (like a "break" statement) makes it stop. Another tip, squares are rectangles too, so there's no need to have a separate function to calculate the perimeter of a square, you can just pass the same value as both width and height. You'll also want to use the raw_input() function instead of the input() function because the latter will try to evaluate the string the user types as an expression, which can be quite messy to deal with if you're not prepared. The last thing I'll mention is some kind of data validation. In this case, raw_input() returns a string and you want to make sure that the user entered a number before you try to add anything. That's what "option = int(user_input)" does, it tries to convert what the user entered into an integer, and if the user entered something that doesn't convert to an integer then a ValueError exception is raised. I don't know if you are familiar with exceptions yet, so this is a trivial introduction into handling them. You'll probably want to do something similar to the values entered by the users for width and height.

[code]
def get_option():
print
print "Please choose an option below"
print "------------------------------"
print "1. Calculate perimeter of a Rectangle."
print "2. Calculate perimeter of a Square."
print "3. Quit."
print
while 1:
try:
option = int(user_input)
return option
except ValueError:
print "Invalid selection. Please try again!"

def rectangle_perimeter(w, h):
return (w * 2) + (h * 2)

while 1:
option = get_option()
if option == 1:
print 'rectangle'
elif option == 2:
print 'square'
elif option == 3:
break
else:
print "Invalid selection. Please try again!"

print "Goodbye!"
[/code]

[size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

[code]
\$ select * from users where clue > 0
no rows returned
[/code]

• : : Hi all
: : I'm new to Python and programmning so easy..
: :
: : I'm struggling with a simple program I wrote to calculate perimeters of squares and rectangles. (yeah I know... but I'm just starting out with practising functions).
: : Once the user has entered an option and found out their perimeter, I can get the menu showing again, but when the select another option it just quits out of the program. My code is below. If someone could point me in the direction of allowing the user to be able to continue selecting options, right up until the actually press 3 to quit. I've been advised about using loop construct and I've looked on this but I'm still confused about to accomplish my task...anyone have some pointers?
:
: Here's a start in the right direction. What you want is a loop that never ends unless you make it end. What's what "while 1" does. "1" is always "True", so the loop will continue forever unless something (like a "break" statement) makes it stop. Another tip, squares are rectangles too, so there's no need to have a separate function to calculate the perimeter of a square, you can just pass the same value as both width and height. You'll also want to use the raw_input() function instead of the input() function because the latter will try to evaluate the string the user types as an expression, which can be quite messy to deal with if you're not prepared. The last thing I'll mention is some kind of data validation. In this case, raw_input() returns a string and you want to make sure that the user entered a number before you try to add anything. That's what "option = int(user_input)" does, it tries to convert what the user entered into an integer, and if the user entered something that doesn't convert to an integer then a ValueError exception is raised. I don't know if you are familiar with exceptions yet, so this is a trivial introduction into handling them. You'll probably want to do something similar to the values entered by the users for width and height.

Thanks very much for the reply, I'm trying to get to grips with what you've said. I'm sure it will come to me... I've ordered a book also on learning Python which I can use along with my growing collection of notes...
Theres pieces in the code I've not came across yet, such as
try:
also where it says while 1:
what does this do/mean? I've came across pieces of code such as
while number<0:
while number !=3 etc.... so just trying to understand what while 1: does

• : Thanks very much for the reply, I'm trying to get to grips with what you've said. I'm sure it will come to me... I've ordered a book also on learning Python which I can use along with my growing collection of notes...
: Theres pieces in the code I've not came across yet, such as
: try:
: also where it says while 1:
: what does this do/mean? I've came across pieces of code such as
: while number<0:
: while number !=3 etc.... so just trying to understand what while 1: does

My favorite Python book so far is "Practical Python" by Magnus Lie Hetland.

"try" is part of a special kind of block. What it does is tell python "try to run the following lines of code". In the example I sent you, the "try" block was followed by an "except" block. Exceptions are like errors. If something "wrong" occurs, an exception is raised by Python. If you don't catch that exception then the program will terminate. So in the example, "except ValueError" means, "if a ValueError occurred in the previous try block, execute the following lines".

A "while" loop always tests some expression for a True or False result. It will continue looping until the expression equals False. In the two examples you provide, "while number < 0" and "while number != 3", you have the keyword "while" followed by an expression. Each time through the loop, Python evaluates "number < 0" or "number != 3" to see if it is still True. If True, the loop runs again, otherwise the loop stops. The number zero is the same as False. Any other number is True. So, "while 1" means "loop forever" because 1 is True forever. Empty strings (like s = '') are False. All other strings are True. Empty lists, tuples, and dicts are False. Lists, tuples, and dicts that contain any number of items are true.

Here's a typical example:

[code]
for line in file('foo.txt'): # line 1
if line.strip(): # line 2
pass # line 3
[/code]

The first line means "execute the following code once for each line in the file named 'foo.txt'. The second line means "if, after stripping all leading and trailing whitespace off, the line has any characters in it then perform the following code. The third line just contains the "pass" keyword which is Python's "do nothing" statement. I've included it here simply for example (because every block must have at least one statement in it). The point I'm trying to make is that the "if" statement is evaluating a string (the result of calling line.strip()) to determine if line 3 should execute.

[size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

[code]
\$ select * from users where clue > 0
no rows returned
[/code]

• My favorite Python book so far is "Practical Python" by Magnus Lie Hetland.
:
: "try" is part of a special kind of block. What it does is tell python "try to run the following lines of code". In the example I sent you, the "try" block was followed by an "except" block. Exceptions are like errors. If something "wrong" occurs, an exception is raised by Python. If you don't catch that exception then the program will terminate. So in the example, "except ValueError" means, "if a ValueError occurred in the previous try block, execute the following lines".
:
: A "while" loop always tests some expression for a True or False result. It will continue looping until the expression equals False. In the two examples you provide, "while number < 0" and "while number != 3", you have the keyword "while" followed by an expression. Each time through the loop, Python evaluates "number < 0" or "number != 3" to see if it is still True. If True, the loop runs again, otherwise the loop stops. The number zero is the same as False. Any other number is True. So, "while 1" means "loop forever" because 1 is True forever. Empty strings (like s = '') are False. All other strings are True. Empty lists, tuples, and dicts are False. Lists, tuples, and dicts that contain any number of items are true.
:
: Here's a typical example:
:
: [code]
: for line in file('foo.txt'): # line 1
: if line.strip(): # line 2
: pass # line 3
: [/code]
:
: The first line means "execute the following code once for each line in the file named 'foo.txt'. The second line means "if, after stripping all leading and trailing whitespace off, the line has any characters in it then perform the following code. The third line just contains the "pass" keyword which is Python's "do nothing" statement. I've included it here simply for example (because every block must have at least one statement in it). The point I'm trying to make is that the "if" statement is evaluating a string (the result of calling line.strip()) to determine if line 3 should execute.
:
: I hope this made sense.

Thank You vey much. I'm printing these replies to keep in my notes folder. I'm sure it will all come easier to me with practise. The book I have ordered is 'Learning Python' by Mark Lutz......ever come across this one?

Thanks again

• : Thank You vey much. I'm printing these replies to keep in my notes folder. I'm sure it will all come easier to me with practise. The book I have ordered is 'Learning Python' by Mark Lutz......ever come across this one?

With a little practice these things will become second-nature to you. It helps, though, if you have experience with other languages. If you're completely new to programming then you'll have to learn the basic concepts at the same time, but Python makes that easy.

Lutz's book was the first one I bought. It's good too, especially for a beginner.

[size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

[code]
\$ select * from users where clue > 0
no rows returned
[/code]

• : : Thank You vey much. I'm printing these replies to keep in my notes folder. I'm sure it will all come easier to me with practise. The book I have ordered is 'Learning Python' by Mark Lutz......ever come across this one?
:
: With a little practice these things will become second-nature to you. It helps, though, if you have experience with other languages. If you're completely new to programming then you'll have to learn the basic concepts at the same time, but Python makes that easy.
:
: Lutz's book was the first one I bought. It's good too, especially for a beginner.
:
:
: [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
:
: [code]
: \$ select * from users where clue > 0
: no rows returned
: [/code]
:
:
Another book that might be useful to you, especially once you get a bit more familliar with python/programming in general is Ptyhon in a Nutshell by Alex Martell. It is an O'Reilly published book and I find that as I code some basic concept seems to elude me and a lot of the time that basic concept is in this book.

Jeff