Consider the following sample problem:
Write a program that prompts the end-user for a number
and checks if the input is indeed a valid number. Your program should continue to prompt the user for a valid number until he enters one.
In this lesson, we'll solve this problem starting with pseudo-code and take it to a complete Python solution. We'll avoid using any library functions to help us, other than len, which tells us the length of a string. Psuedocoding
To pseudocode means to plan out a solution to a problem
in something between natural language and code. It means to not sweat
the syntax details but first figure out the logic of a solution.
A first stab at a solution might involve the following thinking:
We need one loop that continues until the user enters a valid number. We need another loop that walks through the characters entered, checking that each one is a digit.
From this we might get the following pseudo-code solution:
while a valid number has not been input: read the user's input
walk through the characters of the input, checking each
if a character is not a digit, stop and read a new input from the user
if all characters were digits we're done.
If you pseudo-code logic is correct, then next step is converting English to Python, or whatever coding language you're using. If you're new to programming or a language, this isn't necessarily trivial. For the above example, you need to know many things, including:
1. How to read in a string from end-user. 2. How to look at a particular character of a string. 3. How to check if a character in a string is a digit. 4. How to iterate through statements until some arbitrary thing becomes true.
How to read in a string from end-user.Python provides both raw_input() and input() as functions that accept input from the end-user of a program. input() is convenient, because it will automatically evaluate a bunch of typed digits as a number. However, input() cannot be used if you want to validate the user's input: if the end-user types some letters, Python will die and give an ugly error message. raw_input() just puts the characters entered by the end-user into a string, and then you can check them and convert them to whatever type you want. It's more laborious, but good software doesn't allow end-users to see ugly system error messages.
So we'll use raw_input() and our pseudocode is refined to:
while a valid number has not been input: string = raw_input('please enter a number:')
walk through the characters of the input, checking each
if a character is not a digit, stop and read a new input from the user
if all characters were digits we're done.
Looking at characters in a string and checking if they're digitsJust as we can check an element in a list using an index, e.g., list[i], we can do the same with a string. A string is fundamentally a list of characters ended with an end-of-string character (/0).So we can walk through a string, looking at each character, in the same way we walk through a list:
i=0 while i<len(string): #do something with string[i] i=i+1
Checking if a character is a digit is easy once you realize that characters are just ASCII code numbers. The character '0' is ASCII code 48 and the character '9' is ASCII code 57. '1'-'8' are in between. So you can check if a particular character is a digit by writing:
if string[i]>='0' and string[i]<='9'
Note that the following is not valid Python: if string[i]>='0' and <='9'Such 'loose' syntax works in English, but not in Python.
To ask if a particular character is not a digit, just throw a 'not' in front and put parenthesis around the main clause:
if not (string[i]>='0' and string[i]<='9') O.K., now our pseudocode can be further refined:
while a valid number has not been input: string = raw_input('please enter a number:')
i=0 while i<len(string): if not (string[i]>='0' and string[i]<='9'): # not sure what to do yet
i=i+1
if all characters were digits we're done. If not, we need to get new input from the user.
Looping until some arbitrary condition is true.Next, we need to figure out what to do if we get a non-digit character. We should print a message for the user, and then we need to pop-out of the inner loop and jump up to the line where we readinput().
One way to "pop-out" of the inner loop is to make the condition i<len(string) false. We could do this by setting i=len(string):
if not (string[i]>='0' and string[i]<='9'):
print 'You entered an invalid number. Please try again' i=len(string)
Python also provides a 'break' statement that says "I don't care what the loop condition is, lets pop-out of the loop". So the following would work as well:
if not (string[i]>='0' and string[i]<='9'): print 'You entered an invalid number. Please try again'
breakEither one will get the code to stop checking the input, but what then? At that point, if you've determined the input was invalid, you need to get back up to the top and call raw_input() again. Here is where the outer loop comes in-- how do you code, "while a valid number has not been input"?
The answer is to use a boolean variable. A boolean variable is either 'true' or 'false'. In this case, we can use a variable 'validNumber' to represent whether we've read a valid number or not. We'll set it to true before we start checking the string, and false if we get an invalid character. And our outer while loop will just check the value of 'validNumber'. Putting these things together, we get a fully refined solution to our problem:
validNumber=False while not validNumber: string = raw_input('please enter a number:') i=0 validNumber=True while i<len(string): if not (string[i]>='0' and string[i]<='9'): validNumber=False print 'You entered an invalid number. Please try again' break i=i+1
|
|