IT tutorials
 
Database
 

MySQL for Python : Simple Insertion - Changing insertion values dynamically

5/28/2013 7:38:40 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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:

  1. 1. Validate the name column.

  2. 2. Validate the price column.

  1. 3. 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.

Validating the value of name

This function needs to do the following:

  1. 1. Receive the value of the name column.

  2. 2. Check whether it is all letters—for example, no fish has numbers in its market name.

  3. 3. Call query() if necessary.

  1. 4. 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

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)

Querying the user for a correction

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.

Passing fish and price for validation

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)
 
Others
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us