One common reason for programs crashing is bad input.
Input Validation is used to check that any data entered by a user is suitable before it is used by the program.
To avoid making the program too complex, validation routines can be written as subprograms.
In Python, all sub-programs are technically known as functions.
In [ ]:
def my_function():
"""print a bit of the two times table"""
for i in [4,5,6]:
print("{0} times 2 = {1}".format(i, i*2))
print("Part of The 2 Times Table Program")
my_function()
print("\nHere it is again")
my_function()
In [ ]:
def wee_function():
"""print a wee message"""
print()
print("Let's call your function:")
wee_function()
print("Now call it again:")
wee_function()
If you've done it right your output should look something like this:
Let's call your function:
A wee hello message!
Now call it again:
A wee hello message!
The important thing to notice is that you only need to tell Python how to print("A wee hello message") at one point in the program (i.e. in the function definition). You can then call that function multiple times and have it execute your code.
To be more useful, functions can accept arguments. Below, there is a function to print a personalised welcome message.
In [ ]:
def welcome(name):
if name=="Donald Trump":
print("Gonnae no dae that?",name,"just, gonnae no?")
else:
print("Welcome", name, "great things await no doubt.")
print("Welcome to the Welcome Program")
welcome("Boris")
welcome("Donald Trump")
welcome(32)
Continuing with their desire to be useful, functions can return a value. So what does that mean? Here's a couple of examples. We have a function that returns either True or False depending on what day of the week it is, and another function which returns double it's argument.
Run the code and make sure that you understand why what gets printed in which order.
[Is that bad grammar? Don't tell your English teecher].
In [ ]:
def day_of_the_week(day):
if day=="Saturday" or day=="Sunday":
message = "It is the weekend. Yay."
elif day in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]:
message = "It is not the weekend, nay."
else:
message = "You are an idiot."
return message
print(day_of_the_week("Saturday")) #this prints immediately.
day_of_the_week("Sunday") #oops, not doing anything with the return value: destroyed.
response = day_of_the_week("Monday") #this doesn't get printed, yet
print(day_of_the_week("Monster Munch")) #this also prints immediately.
print(response) #now that earlier message gets printed
In [ ]:
def get_yes_or_no(prompt):
"""Input validation returning either Y or N"""
yesno = input(prompt)
yesno = yesno.upper()
while not(yesno in ["Y", "N", "YES", "NO"]):
print("Invalid Input")
yesno = input(prompt)
yesno = yesno.upper()
return yesno[0] # first char of string guaranteed to be Y or N
# Sample program
happy = get_yes_or_no("Are you happy?")
while happy=="N":
print("Be happy")
happy = get_yes_or_no("Are you happy?")
print("I'm happy too. ")
A common validation is to check that only whole numbers within a certain range are entered – a range check. In Python this requires two subprograms:
In [ ]:
def get_integer(prompt):
"""Display prompt and get integer without crashing"""
while True:
try:
i = int(input(prompt))
return i
except ValueError:
print("That is not an integer")
def get_valid_class_size(prompt):
"""Get a class size from 0 to 30. Uses the get_integer function"""
class_size =get_integer(prompt)
while class_size<0 or class_size>30:
print("Invalid - must be from 0 to 30")
class_size = get_integer(prompt)
return class_size
#Sample Program
class1 = get_valid_class_size("How many pupils in Mr T's class?")
class2 = get_valid_class_size("How many pupils in Ms S's class?")
both_classes = class1 + class2
print("Total number of pupils = ", both_classes)
In [ ]:
def get_decimal(prompt):
"""Display prompt and get number without crashing"""
while True:
try:
i = float(input(prompt))
return i
except ValueError:
print("That is not a number")
def get_valid_price(prompt):
"""Get a price from 0.01 to 100.00"""
price = get_decimal(prompt)
while price<0.01 or price>100.00:
print("Invalid - must be from £0.01 to £100.00")
price = get_decimal(prompt)
return price
# Sample program
book1 = get_valid_price("How much is book 1? ")
book2 = get_valid_price("How much is book 2? ")
both_books = book1 + book2
print("Total price £", both_books)
You can now complete tasks 36-38