-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeacademy function challenges for beginners
118 lines (77 loc) · 3.8 KB
/
codeacademy function challenges for beginners
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
This article will help you review Python functions by providing some code challenges involving functions.
Some of these challenges are difficult! Take some time to think about them before starting to code.
You might not get the solution correct on your first try — look at your output, try to find where you’re going wrong, and iterate on your solution.
Finally, if you get stuck, use our solution code! If you “Check Answer” twice with an incorrect solution, you should see an option to get our solution code. However, truly investigate that solution — experiment and play with the solution code until you have a good grasp of how it is working. Good luck!
Function Syntax
As a refresher, function syntax looks like this:
Challenges
We’ve included 5 challenges below. Try to answer all of them and polish up your problem-solving skills!
1. First Three Multiples
Write a function named first_three_multiples() that has one parameter named num.
This function should print the first three multiples of num. Then, it should return the third multiple.
For example, first_three_multiples(7) should print 7, 14, and 21 on three different lines, and return 21.
def first_three_multiples(num):
num1 = num*1
num2 = num*2
num3 = num*3
return num3
# Uncomment these function calls to test your first_three_multiples function:
first_three_multiples(10)
print(first_three_multiples(10))
# should print 10, 20, 30, and return 30
# first_three_multiples(0)
# should print 0, 0, 0, and return 0
2. Tip
Create a function called tip() that has two parameters named total and percentage.
This function should return the amount you should tip given a total and the percentage you want to tip.
def tip(total, percentage):
return total*percentage/100
# Uncomment these function calls to test your tip function:
print(tip(10, 25))
# should print 2.5
print(tip(0, 100))
# should print 0.0
3. Bond, James Bond
Write a function named introduction() that has two parameters named first_name and last_name.
The function should return the last_name, followed by a comma, a space, first_name another space, and finally last_name.
# Write your introduction function here:
def introduction(fn, ln):
return ln + "," + " " + fn + " " + ln
# Uncomment these function calls to test your introduction function:
print(introduction("James", "Bond"))
# should print Bond, James Bond
print(introduction("Maya", "Angelou"))
# should print Angelou, Maya Angelou
4. Dog Years
Some say that every one year of a human’s life is equivalent to seven years of a dog’s life. Write a function named dog_years() that has two parameters named name and age.
The function should compute the age in dog years and return the following string:
# Write your dog_years function here:
def dog_years(name, age):
dog_year = age*7
return name + ", you are " + str(dog_year) + " years old in dog years"
# Uncomment these function calls to test your dog_years function:
print(dog_years("Lola", 16))
# should print "Lola, you are 112 years old in dog years"
print(dog_years("Baby", 0))
# should print "Baby, you are 0 years old in dog years"
5. All Operations
Create a function named lots_of_math(). This function should have four parameters named a, b, c, and d. The function should print 3 lines and return 1 value.
First, print the sum of a and b.
Second, print d subtracted from c.
Third, print the first number printed, multiplied by the second number printed.
Finally, return the third number printed mod a.
# Write your lots_of_math function here:
def lots_of_math(a,b,c,d):
a_1 = a+b
a_2 = c-d
a_3 = a_1*a_2
print(a_1)
print(a_2)
print(a_3)
return a_3%a
# Uncomment these function calls to test your lots_of_math function:
print(lots_of_math(1, 2, 3, 4))
# should print 3, -1, -3, 0
print(" ------- ")
print(lots_of_math(1, 1, 1, 1))
# should print 2, 0, 0, 0