8000 Finish exercises7 · Pcodias/intro-to-python@710e709 · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit 710e709

Browse files
author
Replit user
committed
Finish exercises7
1 parent 823943a commit 710e709

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

session_07/exercises_7.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,52 @@
22

33
## Section A
44
# 1. Write a function that prints your name
5+
def print_name():
6+
print("Paula")
57

68

9+
print_name()
10+
711

812
# 2. Write a function that accepts a name as a parameter and prints "Hello, <name>".
13+
def hello(name):
14+
print("Hello, " + name + "!")
15+
16+
List = ["Alice", "Bob", "Charlie"]
17+
hello("Alice")
18+
hello("Bob")
19+
hello("Charlie")
920

1021

1122

1223
# 3. Loop through the list ["Alice", "Bob", "Charlie"] and call the function you just wrote.
24+
def hello(name):
25+
print("Hello, " + name + "!")
26+
27+
names_list = ["Alice", "Bob", "Charlie"]
28+
29+
for name in names_list:
30+
hello(name)
1331

1432

1533

1634
# 4. Write a function that prints the area of two passed in parameters.
35+
def area(a, b):
36+
print("The area is " + str(a * c))
37+
1738

39+
area(1, 3)
1840

1941

2042
# 5. Write a function called 'print_list' that accepts a list as a parameter and then prints out each item of the list.
43+
def print_list(shapes):
44+
for shape in shapes:
45+
print(shape)
46+
47+
print_list(["circle", "rectangle", "square"])
48+
49+
50+
print_list(["circle", "rectangle", "square"])
2151

2252

2353

@@ -26,18 +56,57 @@
2656
# 2. If they are between 11 and 16, print "You can can come to this school".
2757
# 3. If they are over 16, print 'You're too old for school".
2858
# 4. If they are 0, print "You're not born yet!".
59+
def school(age):
60+
if age < 11:
61+
print("You're too young to go to this school.")
62+
elif age >= 11 and age <= 16:
63+
print("You can come to this school.")
64+
elif age > 16:
65+
print("You're too old for school.")
66+
elif age == 0:
67+
print("You're not born yet.")
68+
else:
69+
print("You didn't pick a number.")
70+
2971

72+
school(16)
73+
school(0)
74+
school(19)
3075

3176

3277

3378
# <---------------------------------------------------------------------------------------------->
3479

3580
## Section B
3681
# 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+
def is_odd(number):
83+
if number % 2 == 1:
84+
return True
85+
else:
86+
return False
87+
88+
is_odd(6)
89+
is_odd(5)
3790

3891

3992

4093
# 2. Write a function that accepts a word and returns it backwards, e.g. 'hello' -> 'olleh'.
94+
def reverse_word(name):
95+
#METHOD 1
96+
new_string = ""
97+
name_length = len(name)
98+
while name_length != 0:
99+
name_length -= 1
100+
new_string += name[name_length]
101+
return new_string
102+
103+
#METHOD 2
104+
# reverse = name[::-1]
105+
# print(reverse)
106+
107+
108+
109+
reverse_word("hello")
41110

42111

43112

@@ -49,25 +118,98 @@
49118
# **
50119
# *
51120
# ```
121+
def print_stars(x):
122+
star = ""
123+
for y in range(0, x):
124+
star = star + "*"
125+
print(star)
126+
if x > 1:
127+
print_stars(x-1)
128+
129+
130+
print_stars(10)
52131

53132

54133

55134
# 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+
def padlock(user_guess):
136+
137+
pin = 8450
138+
if pin == user_guess:
139+
print("Unlock")
140+
else:
141+
print("Locked")
142+
143+
144+
padlock(3423)
145+
padlock(8450)
56146

57147

58148

59149
# 5. Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter).
60150
# For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
151+
def multiples_3_and_5(limit):
152+
153+
sum = 0
154+
155+
for i in range (0, limit+1):
156+
if limit < 0:
157+
print("The limit must be greater than 0")
158+
if i % 3 == 0:
159+
sum += i
160+
elif i % 5 == 0:
161+
sum += i
162+
163+
print(sum)
61164

165+
multiples_3_and_5(20)
166+
62167

63168

64169
# 6. Write a function called is_prime() that accepts a number and return True or False if the number of prime or not.
170+
def is_prime(num):
171+
count = 0
172+
173+
for i in range(2, num):
174+
if(num % i == 0):
175+
count = count + 1
176+
break
65177

178+
if (count == 0 and num != 1):
179+
print(str(num) + " is a Prime Number")
180+
return True
181+
else:
182+
print(str(num) +" is not a Prime Number")
183+
return False
184+
185+
is_prime(6)
186+
is_prime(99)
187+
is_prime(83)
66188

67189

68190
# 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+
def pallindrome(word):
192+
reverse_word = word[::-1]
193+
if word == reverse_word:
194+
print(True)
195+
else:
196+
print(False)
197+
198+
pallindrome("strognsgag")
199+
pallindrome("abba")
69200

70201

71202

72203
# 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.
73204
# Tip - you may want to format your sentence so it is all lower case, and .replace() to get rid of white spaces.
205+
def pallindrome_sen(sentence):
206+
formatted_sen = sentence.lower()
207+
new_sentence = formatted_sen.replace(' ', '')
208+
rev_sentence = new_sentence[::-1]
209+
if new_sentence == rev_sentence:
210+
print(True)
211+
else:
212+
print(False)
213+
214+
pallindrome_sen("The cat sat on the mat")
215+
pallindrome_sen("A nut for a jar of tuna")

0 commit comments

Comments
 (0)
0