Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 2nd, 2006, 5:43 PM   #11
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Volkan
i used the bit of code you gave which sued the whil command etc.
but i dont understand how to use it, i put it in, it asks to continue the loop i press 1 to continue and it just ends the script..
im guessing i need to define where it needs to loop to?
Are you saying that my example code didn't run correctly, or are you saying that you can't get your code to work correctly with the while loop?

If it's the latter, could you post up what you have so far?
Arevos is offline   Reply With Quote
Old Feb 2nd, 2006, 8:06 PM   #12
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
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!
Dietrich is offline   Reply With Quote
Old Feb 3rd, 2006, 5:58 PM   #13
Volkan
Newbie
 
Join Date: Feb 2006
Location: London-Uk
Posts: 7
Rep Power: 0 Volkan is on a distinguished road
Send a message via MSN to Volkan
whats that got to dow ith anything?
Volkan is offline   Reply With Quote
Old Feb 3rd, 2006, 6:05 PM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 4th, 2006, 5:07 AM   #15
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by DaWei
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.
I'm not quite sure I follow you. What do you mean?
Arevos is offline   Reply With Quote
Old Feb 4th, 2006, 6:43 AM   #16
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Volkan
whats that got to dow ith anything?
I think it might be best to take you through an example of how to thing about programming. Programming requires a certain mindset. A way of dismantling a problem and turning it into a solution.

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 key to programming is to keep breaking down a problem into smaller and smaller details. This is sometimes not a simple process, but with practice, you can get used to thinking about how to turn something into a program.

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 shape
That's all fine and good. But pseudocode doesn't actually do anything. So lets change this into Python code:
display_program_title()
while True:
    shape = ask_user_which_shape()
    measurements = ask_user_measurements_of(shape)
    display_area_of(shape, measurements)
This is a big step. And probably not altogether obvious how I got from the pseudocode to the actual Python code. There's three things to observe:

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
This looks okay, but what happens if the user enters in an invalid choice? A good way to deal with this would be to keep asking the user for his choice, until the user makes a correct one:
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
Again, we have what appears to be a look. The word "repeat" is a dead giveaway, and we already know how to handle loops. But unlike our previous loop, we want a way to escape it:
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 loop
Next we convert it into Python code:
def 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."
Another three functions! This might be getting a little tiresome, but our goal is in sight. These functions are a lot simpler than all the previous ones. Let's take display_shape_choices - that's just printing out some text to the screen. Indeed, it's so simple that it's irreducable. It's so simple that it needs no more functions. We need not even bother with pseudocode:
def display_shape_choices():
    print "Please select a shape:"
    print "1. Rectangle"
    print "2. Circle"
Easy enough. Now for ask_user_for_choice. Again, this is pretty simple:
def ask_user_for_choice():
    return raw_input("> ")
So simple, in fact, it doesn't really need it's own function:
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."
The final function is is_valid_choice. Earlier I suggested using ValueError and int - but on reflection, I suspect that's too complicated, when something simple will do:
1. If choice is a 1 or a 2, then return true.
2. If it's not, return false
Into Python:
def is_valid_choice(choice):
    if choice == "1" or choice == "2":
        return True
    else
        return False
Note that "1" and "2" are strings, because the input from the user will also be a string. We can simplify this a bit more, too:
def 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"
This is a technique for developing procedual program. More advanced programs use Object Orientated (OO) techniques, but it's probably best to start with functions and move onto classes and objects, rather than to try everything at once.

And that's programming.
Arevos is offline   Reply With Quote
Old Feb 4th, 2006, 7:13 AM   #17
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Feb 4th, 2006, 7:32 AM   #18
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Jessehk
I just have to give you a reputation point for that post, Arevos
Thanks!
Arevos is offline   Reply With Quote
Old Feb 4th, 2006, 8:23 AM   #19
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Snap :-)
Cerulean is offline   Reply With Quote
Old Feb 4th, 2006, 9:08 AM   #20
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
Great response. One thing I wonder, though - could

def is_valid_choice(choice):
     return choice in ["1", "2"]
ala "Pascal set style", be considered better than

def is_valid_choice(choice):
     return choice == "1" or choice == "2"
-? It's certainly easier to extend.
Klipt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC