Just because user input is not valid, it does not mean we should scrap it and ask the user for a new input. Rather, we can accept an entire statement, assign what values will fit and come back to the user to correct data that will not.
To do this, we need to import the string module and define three functions to do the following:
- Validate the
name
column. - Validate the
price
column. - Query the user for a correction.
After defining the functions, we pass the user values to the first two functions, we then pass the user values in turn to the first two functions which then calls the third function only if the data does not check out.
Simply append string
to the import line for the program or add the following line below it:
import string
Now, let's define the three functions.
This function needs to do the following:
- Receive the value of the
name
column. - Check whether it is all letters—for example, no fish has numbers in its market name.
- Call
query()
if necessary. - Return the
name
value to the main program, corrected if necessary.
The first is accomplished with the definition line:
def valid_name(name):
We then accomplish the rest of the specified functionality with an if...else
statement:
if name.isalpha() is False: fish = query("name", name, "alpha") else: fish = name
Finally, we return the value of fish
to the main program:
return(fish)
Validating the value of price requires similar functionality. Here is the function that we need for the task:
def valid_price(price): if price.isdigit() is False: price = query("price", price, "digit") else: price = price return(price)
As you can tell from the calls in the preceding functions, this function will take three arguments and query the user for a correction according to them. Our definition thus begins:
def query(column, value, kind):
For kind
, we will use two different possible values: alpha and digit. Depending on which one is used, this function will behave differently. For validating the alpha character of name value, we use the following if... clause
:
if kind == "alpha": print "For %s, you input %s. This is not a valid value for column %s. Please enter the name of the fish in the appropriate format." %(column, value, column) new_value = raw_input("New name: ") new_value = valid_name(new_value) return (new_value)
If type is not alpha but digit, we use an elif
clause to continue with the user query:
elif kind == "digit": print "For %s, you input %s. This is not a valid price. Please enter the price in the appropriate format." %(column, value) new_price = raw_input("New price: ") new_price = valid_price(new_price) return (new_price)
Finally, because this function interacts with the user, we want to ensure that it cannot be called from another program or with any other values for type other than alpha or digital. To affect this in the shortest amount of code possible, we use a simple else
statement.
else: return -1
We return the value -1
here, in an effort to ensure that the erroneous call does not go unnoticed.
Having defined the three functions, we now need to call them and to pass to them the values for fish and price. We therefore put this code just after assigning the values of sys.argv[1]
and sys.argv[2]
to fish
and price
, respectively.
fish = valid_name(fish) price = valid_price(price)