Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.

Similar presentations


Presentation on theme: "More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1."— Presentation transcript:

1 More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

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. 2

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 3

4 Example Main File from months import * while(True): month = input("please enter a month: ") if (month == 'q'): break result = season(month) print(result) print() print("exiting") 4

5 Example of Desired Output please enter a month: June summer please enter a month: May spring please enter a month: ww unknown month please enter a month: q exiting 5

6 Wrong Solution #1 def 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? 6

7 Wrong Output #1 please enter a month: June summer None please enter a month: May spring None please enter a month: ww unknown month None please enter a month: q exiting 7

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. 8

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") 9

10 Wrong Solution #2 def 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? 10

11 Wrong Output #2 please enter a month: June summer please enter a month: May spring please enter a month: er unknown month please enter a month: q exiting 11

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. 12

13 Wrong Solution #3 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("you entered an unknown month") What is wrong with this? 13

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. 14

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 15

16 Example Main File from 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") 16

17 Wrong Solution def multiple_of_7(number): if (number % 7 == 0): return "True" else: return "False" What is wrong with this? 17

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. 18

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. 19

20 A Better Test File from 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") 20

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. 21

22 Correct Solution def multiple_of_7(number): if (number % 7 == 0): return True else: return False 22

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 --> True type(4) is float --> False a = 'hello' type(a) is str --> True 23

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 24

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 …"). 25

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 26

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). 27

28 Names of Variables This code works. – it doesn't really matter what test_function does, it is just an example. 28 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? 29 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). 30 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. 31 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()


Download ppt "More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1."

Similar presentations


Ads by Google