View
228
Download
1
Category
Preview:
Citation preview
1
More on Functions
CSE 1310 – Introduction to Computers and ProgrammingVassilis Athitsos
University of Texas at Arlington
2
Common Mistakes with Functions
• Printing instead of returning.• Returning the wrong value.• Asking the user for input instead of using arguments.• Not taking the arguments that were specified.• Including extra code in your submissions ( like testing
code).• All these problems will be penalized severely.
– I have not found a better way to convince people to avoid these problems.
3
An Example Assignment Task• Write a function called season(month) that satisfies the
following specs:– It takes one argument, called month.– Do not worry about what happens if month is not a string.– If month is "March", "April", or "May", the function should return
"spring".– If month is "June", "July", or "August", the function should return
"summer".– If month is "September", "October", or "November", the function
should return "fall".– If month is "December", "January", or "February", the function should
return "winter".– In all other cases, the function should return the string "unknown
month"
• Save your code in a file called months.py
4
Example Main Filefrom months import *
while(True): month = input("please enter a month: ") if (month == 'q'): break result = season(month) print(result) print()
print("exiting")
5
Example of Desired Outputplease enter a month: Junesummer
please enter a month: Mayspring
please enter a month: wwunknown month
please enter a month: qexiting
6
Wrong Solution #1def season(month) : if (month == "March") or (month == "April") or (month == "May"): print("spring") elif (month == "June") or (month == "July") or (month == "August"): print("summer") elif (month == "September") or (month == "October") or (month == "November"): print("fall") elif (month == "December") or (month == "January") or (month == "February"): print("winter") else: print("unknown month")
• What is wrong with this?
7
Wrong Output #1please enter a month: JunesummerNone
please enter a month: MayspringNone
please enter a month: wwunknown monthNone
please enter a month: qexiting
8
What Is Wrong with Solution #1
• It does not return anything, it just prints stuff.• There are two ways to tell that something is
wrong:– 1: look at the function. It should be clear that
nothing is returned, which violates the specs.– 2: look at the output. There are extra lines with
"None". Even if you do not understand why (which you should), it is clear that this output does not match the desired output.
9
Correct Solution
def season(month) : if (month == "March") or (month == "April") or (month == "May"): return("spring") elif (month == "June") or (month == "July") or (month == "August"): return("summer") elif (month == "September") or (month == "October") or (month == "November"): return("fall") elif (month == "December") or (month == "January") or (month == "February"): return("winter") else: return("unknown month")
10
Wrong Solution #2def season(month) : month = input("please enter a month: ") if (month == "March") or (month == "April") or (month == "May"): return("spring") elif (month == "June") or (month == "July") or (month == "August"): return("summer") elif (month == "September") or (month == "October") or (month == "November"): return("fall") elif (month == "December") or (month == "January") or (month == "February"): return("winter") else: return("unknown month")
• What is wrong with this?
11
Wrong Output #2please enter a month: Juneplease enter a month: Junesummer
please enter a month: Mayplease enter a month: Mayspring
please enter a month: erplease enter a month: erunknown month
please enter a month: qexiting
12
What Is Wrong with Solution #2
• It does not use the argument, it just asks the user again to enter a month.
• Again, there are two ways to tell that something is wrong:– 1: look at the function, see that the first line
overwrites the value of the argument.– 2: look at the output, the program is asking twice
for each month.
13
Wrong Solution #3def season(month) : if (month == "March") or (month == "April") or (month == "May"): return("spring") elif (month == "June") or (month == "July") or (month == "August"): return("summer") elif (month == "September") or (month == "October") or (month == "November"): return("fall") elif (month == "December") or (month == "January") or (month == "February"): return("winter") else: return("you entered an unknown month")
• What is wrong with this?
14
What Is Wrong with Solution #3
• When the month is not recognized, it returns "you entered an unknown month".– It should be returning "unknown month".
• Yes, this is a big deal.• As a programmer, you must learn to respect the
specs.• Otherwise, you will:
– not be able to work in large projects with many other programmers.
– not be able to give customers what they asked for and what you signed a contract for.
15
Another Example Assignment Task• Write a function called multiple_of_7(number) that
satisfies the following specs:– It takes one argument, called number.– Do not worry about what happens if number is not an int
or float.– If the number is a multiple of seven, the function should
return Boolean value True.– Otherwise the function should return Boolean value False.
• Save your code in a file called multiple7.py
16
Example Main Filefrom multiple7 import *
while(True): number_text = input("please enter an integer: ") if (number_text == 'q'): break number = int(number_text) result = multiple_of_7(number) print(result) print()
print("exiting")
17
Wrong Solution def multiple_of_7(number): if (number % 7 == 0): return "True" else: return "False"
• What is wrong with this?
18
Wrong Solution def multiple_of_7(number): if (number % 7 == 0): return "True" else: return "False"
• What is wrong with this?• If you test it with the provided test file, it will match
the desired output.• However, this is still a wrong solution.• This is a case where the test file does NOT do enough
testing to discover the problem.
19
What Is Wrong with Solution
• It does not return a Boolean, but a string.• The specs explicitly say that the function
should return a Boolean value.• Again, this is a big deal, since the function
explicitly violates the specs.
20
A Better Test Filefrom multiple7 import *
while(True): number_text = input("please enter an integer: ") if (number_text == 'q'): break number = int(number_text) result = multiple_of_7(number) if not(type(result) is bool): print("specs violation: result is a Boolean:") continue print(result) print()
print("exiting")
21
On Test Files
• I will be providing you with test files on many assignment tasks, to help you test the code.
• However, testing your code is primarily YOUR responsibility.
• Why?– whose responsibility would it be in real life? Your
colleagues? Your clients?
• Failing my test files means that the code is incorrect.• Passing my test files doesn't always mean that the
code is correct.
22
Correct Solution def multiple_of_7(number): if (number % 7 == 0): return True else: return False
23
Checking Types of Variables
type(variable) is X
• The above expression returns True if and only if the variable has type X.
• Examples:type(4) is int --> Truetype(4) is float --> Falsea = 'hello'type(a) is str --> True
24
Checking Types of Variables
• Checking types of variables is very useful, for making sure that a function does not crash.
• Example: what if number is a string?
def multiple_of_7(number): if (number % 7 == 0): return True else: return False
25
Checking Types of Variables• Checking types of variables is very useful, for making
sure that a function does not crash. • Example: what if number is a string?
def multiple_of_7(number): if (number % 7 == 0): return True else: return False
• The function will crash.• This is OK ONLY if the specs allow it ( if they say "do
not worry what happens if …").
26
Checking Types of Variables• Suppose the specs say "if number is not an int, then
return None".• What do we do to comply with the specs?
def multiple_of_7(number): if (number % 7 == 0): return True else: return False
27
Checking Types of Variables• Suppose the specs say "if number is not an int, then
return None".• What do we do to comply with the specs?
def multiple_of_7(number): if not(type(number) is int): return None if (number % 7 == 0): return True else: return False
• Checking types is not needed for assignment 4 (will be needed in future assignments).
28
Names of Variables
• This code works.– it doesn't really matter what test_function does, it
is just an example.
def test_function(x, y, z): temporary = x*x + y*y; if (z < 0): return temporary - z*z elif (z == 0): return temporary - 10 else: return temporary + z*z
while(True): text = input("enter number #1: ") if (text == 'q'): break x = int(text)
y = int(input("enter number #2: ")) z = int(input("enter number #3: "))
result = test_function(x, y, z) print(result) print()
29
Names of Variables
• Does this work?
def test_function(x, y, z): temporary = x*x + y*y; if (z < 0): return temporary - z*z elif (z == 0): return temporary - 10 else: return temporary + z*z
while(True): text = input("enter number #1: ") if (text == 'q'): break a = int(text)
b = int(input("enter number #2: ")) c = int(input("enter number #3: "))
result = test_function(x, y, z) print(result) print()
30
Names of Variables
• Does this work?– No! The green line gives an error (x, y, z are not
defined).
def test_function(x, y, z): temporary = x*x + y*y; if (z < 0): return temporary - z*z elif (z == 0): return temporary - 10 else: return temporary + z*z
while(True): text = input("enter number #1: ") if (text == 'q'): break a = int(text)
b = int(input("enter number #2: ")) c = int(input("enter number #3: "))
result = test_function(x, y, z) print(result) print()
31
Names of Variables
• This also works.• The names of the variables passed in as arguments when you
call a function may or may not be the same as the names of the arguments in the function definition.
def test_function(x, y, z): temporary = x*x + y*y; if (z < 0): return temporary - z*z elif (z == 0): return temporary - 10 else: return temporary + z*z
while(True): text = input("enter number #1: ") if (text == 'q'): break a = int(text)
b = int(input("enter number #2: ")) c = int(input("enter number #3: "))
result = test_function(a, b, c) print(result) print()
Recommended