![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
If it's the latter, could you post up what you have so far? |
|
|
|
|
|
|
#12 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Something like this just came up on another forum:
def check_input(low, high):
"""check the input to be an integer number within a given range"""
while True:
try:
a = int(raw_input("Enter a integer number between %d and %d: " % (low, high)))
if low <= a <= high:
return a
except ValueError:
print "Hey, I said integer number!"
# expect an integer number between 1 and 50
print check_input(1, 50)
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#13 |
|
Newbie
|
whats that got to dow ith anything?
|
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Keystrokes are codes. If you ask for a number and the codes won't convert to a number, what do you suppose happens? Top ten answers on the board, SHOW ME "WORKS ANYWAY." Bzzzzzzzzzzzzzzzzzzzzzzzzzzzzz, bad answer, bad answer.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#15 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
|
|
|
|
|
|
|
#16 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Let's take your problem. You want to create a program that calculates the area of different shapes for the user. So lets break it down into steps called pseudocode: 1. Display program title 2. Ask user which shape he wants to find the area of 3. Ask the user the measurements of the shape 4. Display the area of the shape 5. Go to the beginning of the program again. The first thing to notice is that the program wants to repeat. When something needs to repeat, then in programming, we need a loop. With this in mind, we can rewrite the above pseudocode: 1. Display program title
Keep repeating the following steps:
2. Ask user which shape he wants to find the area of
3. Ask the user the measurements of the shape
4. Display the area of the shapedisplay_program_title()
while True:
shape = ask_user_which_shape()
measurements = ask_user_measurements_of(shape)
display_area_of(shape, measurements)First, that I've replaced the "Keep repeating" pseudocode with "while True". This is Python's way of telling a piece of code to keep repeating. Second, that I've introduced the 'shape' and measurement variables to record what shape the user calls. Whenever data needs to be recorded, a variable is required to store this data. Third, that I've turned all my four steps into functions. "Display the area of the shape" becomes "display_area_of(shape)". It all looks rather neat, and it's perfectly valid Python code, but there's a problem: the functions aren't actually defined anywhere. Were we to attempt to run this program, Python would choke and die with an error. That's a pretty big problem. But notice now, that instead of one big problem, we have four smaller ones. We've successfully reduced the problem into pieces. And for each of these pieces, we repeat the same process to reduce the problem still further. Let's take the second function, ask_user_which_shape, and work out some pseudocode for it: 1. Display the choices to the user 2. Ask the user for his choice 3. Return the user's choice of shape 1. Display the shape choices to the user 2. Ask the user for his choice 3. If the choice is invalid, then repeat step 2 until the choice is valid 4. Return the user's choice of shape 1. Display the choices to the user
Keep repeating the following steps:
2. Ask the user for his choice
If the choice is valid:
3. Return the user's choice and end the function
Else
4. Display "Not a valid choice" and continue the loopdef ask_user_which_shape():
display_shape_choices()
while True:
choice = ask_user_for_choice()
if is_valid_choice(choice):
return choice
else:
print "Not a valid choice."def display_shape_choices():
print "Please select a shape:"
print "1. Rectangle"
print "2. Circle"def ask_user_for_choice():
return raw_input("> ")def ask_user_which_shape():
display_shape_choices()
while True:
choice = raw_input("> ")
if is_valid_choice(choice):
return choice
else:
print "Not a valid choice."1. If choice is a 1 or a 2, then return true. 2. If it's not, return false def is_valid_choice(choice):
if choice == "1" or choice == "2":
return True
else
return Falsedef is_valid_choice(choice):
return choice == "1" or choice == "2"With these three functions sorted, we now, finally, have a working ask_user_which_shape: def ask_user_which_shape():
display_shape_choices()
while True:
choice = raw_input("> ")
if is_valid_choice(choice):
return choice
else:
print "Not a valid choice."
def display_shape_choices():
print "Please select a shape:"
print "1. Rectangle"
print "2. Circle"
def is_valid_choice(choice):
return choice == "1" or choice == "2"And that's programming. |
|
|
|
|
|
|
#17 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
I just have to give you a reputation point for that post, Arevos (I know nobody cares, but it shows the person that somebody appreciated their work).
![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#18 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
![]() |
|
|
|
|
|
|
#19 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Snap :-)
|
|
|
|
|
|
#20 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Great response. One thing I wonder, though - could
def is_valid_choice(choice):
return choice in ["1", "2"]def is_valid_choice(choice):
return choice == "1" or choice == "2" |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|