You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 1. Write a function called is_odd that will return True or False if the integer passed as a parameter is odd (hint: x % 2 will return true for all odd numbers).
82
+
defis_odd(number):
83
+
ifnumber%2==1:
84
+
returnTrue
85
+
else:
86
+
returnFalse
87
+
88
+
is_odd(6)
89
+
is_odd(5)
37
90
38
91
39
92
40
93
# 2. Write a function that accepts a word and returns it backwards, e.g. 'hello' -> 'olleh'.
94
+
defreverse_word(name):
95
+
#METHOD 1
96
+
new_string=""
97
+
name_length=len(name)
98
+
whilename_length!=0:
99
+
name_length-=1
100
+
new_string+=name[name_length]
101
+
returnnew_string
102
+
103
+
#METHOD 2
104
+
# reverse = name[::-1]
105
+
# print(reverse)
106
+
107
+
108
+
109
+
reverse_word("hello")
41
110
42
111
43
112
@@ -49,25 +118,98 @@
49
118
# **
50
119
# *
51
120
# ```
121
+
defprint_stars(x):
122
+
star=""
123
+
foryinrange(0, x):
124
+
star=star+"*"
125
+
print(star)
126
+
ifx>1:
127
+
print_stars(x-1)
128
+
129
+
130
+
print_stars(10)
52
131
53
132
54
133
55
134
# 4. Create a padlock function. You need to be able to pass in a passcode and if its correct it should return "Unlock", else "Locked".
135
+
defpadlock(user_guess):
136
+
137
+
pin=8450
138
+
ifpin==user_guess:
139
+
print("Unlock")
140
+
else:
141
+
print("Locked")
142
+
143
+
144
+
padlock(3423)
145
+
padlock(8450)
56
146
57
147
58
148
59
149
# 5. Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter).
60
150
# For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
151
+
defmultiples_3_and_5(limit):
152
+
153
+
sum=0
154
+
155
+
foriinrange (0, limit+1):
156
+
iflimit<0:
157
+
print("The limit must be greater than 0")
158
+
ifi%3==0:
159
+
sum+=i
160
+
elifi%5==0:
161
+
sum+=i
162
+
163
+
print(sum)
61
164
165
+
multiples_3_and_5(20)
166
+
62
167
63
168
64
169
# 6. Write a function called is_prime() that accepts a number and return True or False if the number of prime or not.
170
+
defis_prime(num):
171
+
count=0
172
+
173
+
foriinrange(2, num):
174
+
if(num%i==0):
175
+
count=count+1
176
+
break
65
177
178
+
if (count==0andnum!=1):
179
+
print(str(num) +" is a Prime Number")
180
+
returnTrue
181
+
else:
182
+
print(str(num) +" is not a Prime Number")
183
+
returnFalse
184
+
185
+
is_prime(6)
186
+
is_prime(99)
187
+
is_prime(83)
66
188
67
189
68
190
# 7. Write a function that checks to see if a string is a pallindrome, if it is, it will return True and False if it is not.
191
+
defpallindrome(word):
192
+
reverse_word=word[::-1]
193
+
ifword==reverse_word:
194
+
print(True)
195
+
else:
196
+
print(False)
197
+
198
+
pallindrome("strognsgag")
199
+
pallindrome("abba")
69
200
70
201
71
202
72
203
# 8. Write a function that checks to see if a sentence is a pallindrome, if it is, it will return True and False if it is not.
73
204
# Tip - you may want to format your sentence so it is all lower case, and .replace() to get rid of white spaces.
0 commit comments