File tree Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Expand file tree Collapse file tree 2 files changed +30
-2
lines changed Original file line number Diff line number Diff line change 6
6
7
7
# YOUR CODE HERE
8
8
9
+ def f1 (a , b ):
10
+ return a + b
11
+
9
12
print (f1 (1 , 2 ))
10
13
11
14
# Write a function f2 that takes any number of integer arguments and prints the
12
15
# sum. Google for "python arbitrary arguments" and look for "*args"
13
16
14
17
# YOUR CODE HERE
15
18
19
+ def f2 (* args ):
20
+ res = 0
21
+ for num in args :
22
+ res += num
23
+ return res
24
+
16
25
print (f2 (1 )) # Should print 1
17
26
print (f2 (1 , 3 )) # Should print 4
18
27
print (f2 (1 , 4 , - 12 )) # Should print -7
21
30
a = [7 , 6 , 5 , 4 ]
22
31
23
32
# What thing do you have to add to make this work?
24
- print (f2 (a )) # Should print 22
33
+ print (f2 (* a )) # Should print 22
25
34
26
35
# Write a function f3 that accepts either one or two arguments. If one argument,
27
36
# it returns that value plus 1. If two arguments, it returns the sum of the
28
37
# arguments. Google "python default arguments" for a hint.
29
38
30
39
# YOUR CODE HERE
31
40
41
+ def f3 (a , b = None ):
42
+ if not b :
43
+ return int (a ) + 1
44
+ else :
45
+ return a + b
46
+
32
47
print (f3 (1 , 2 )) # Should print 3
33
48
print (f3 (8 )) # Should print 9
34
49
42
57
# Google "python keyword arguments".
43
58
44
59
# YOUR CODE HERE
60
+ def f4 (** kwargs ):
61
+ print (kwargs )
45
62
46
63
# Should print
47
64
# key: a, value: 12
60
77
}
61
78
62
79
# What thing do you have to add to make this work?
63
- f4 (d )
80
+ f4 (** d )
Original file line number Diff line number Diff line change 1
1
# Write a function is_even that will return true if the passed-in number is even.
2
2
3
3
# YOUR CODE HERE
4
+ def is_even (num ):
5
+ if num % 2 == 0 :
6
+ return True
7
+ else :
8
+ return False
9
+
4
10
5
11
# Read a number from the keyboard
6
12
num = input ("Enter a number: " )
10
16
11
17
# YOUR CODE HERE
12
18
19
+ is_even = is_even (num )
20
+ if is_even == True :
21
+ print ('its even!' )
22
+ else :
23
+ print ('Its odd!' )
You can’t perform that action at this time.
0 commit comments