8000 args done · cmdncode/Intro-Python-I@e25d9a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit e25d9a0

Browse files
committed
args done
1 parent 199d752 commit e25d9a0

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/args.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66

77
# YOUR CODE HERE
88

9+
def f1(a, b):
10+
return a + b
11+
912
print(f1(1, 2))
1013

1114
# Write a function f2 that takes any number of integer arguments and prints the
1215
# sum. Google for "python arbitrary arguments" and look for "*args"
1316

1417
# YOUR CODE HERE
1518

19+
def f2(*args):
20+
res = 0
21+
for num in args:
22+
res += num
23+
return res
24+
1625
print(f2(1)) # Should print 1
1726
print(f2(1, 3)) # Should print 4
1827
print(f2(1, 4, -12)) # Should print -7
@@ -21,14 +30,20 @@
2130
a = [7, 6, 5, 4]
2231

2332
# 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
2534

2635
# Write a function f3 that accepts either one or two arguments. If one argument,
2736
# it returns that value plus 1. If two arguments, it returns the sum of the
2837
# arguments. Google "python default arguments" for a hint.
2938

3039
# YOUR CODE HERE
3140

41+
def f3(a, b=None):
42+
if not b:
43+
return int(a) + 1
44+
else:
45+
return a + b
46+
3247
print(f3(1, 2)) # Should print 3
3348
print(f3(8)) # Should print 9
3449

@@ -42,6 +57,8 @@
4257
# Google "python keyword arguments".
4358

4459
# YOUR CODE HERE
60+
def f4(**kwargs):
61+
print(kwargs)
4562

4663
# Should print
4764
# key: a, value: 12
@@ -60,4 +77,4 @@
6077
}
6178

6279
# What thing do you have to add to make this work?
63-
f4(d)
80+
f4(**d)

src/functions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Write a function is_even that will return true if the passed-in number is even.
22

33
# YOUR CODE HERE
4+
def is_even (num):
5+
if num % 2 == 0:
6+
return True
7+
else:
8+
return False
9+
410

511
# Read a number from the keyboard
612
num = input("Enter a number: ")
@@ -10,3 +16,8 @@
1016

1117
# YOUR CODE HERE
1218

19+
is_even = is_even(num)
20+
if is_even == True:
21+
print('its even!')
22+
else:
23+
print('Its odd!')

0 commit comments

Comments
 (0)
0