-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 3
203 lines (169 loc) · 4.28 KB
/
Day 3
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#Day 3 Challenge focused on fixing code when the items are not intialized
#Code Fix #1
#Original Code
def count_down(start_number):
while (current > 0):
print(current)
current -= 1
print("Zero!")
count_down(3)
#Fixed Code
def count_down(start_number):
current = 3
while (current > 0):
print(current)
current -= 1
print("Zero!")
count_down(3)
#Output 3, 2, 1, Zero!
#Code Fix #2
#Original Code
x = 1
sum = 0
while x < 10:
sum = sum + x
x = x + 1
product = 1
while x < 10:
product = product * x
x = x + 1
print(sum, product)
#Ouput 45 1-->incorrect because x for product has not been defined for the second block, so x=10 for the second block which is false
#Correct Code
x = 1
sum = 0
while x < 10:
sum = sum + x
x = x + 1
product = x
while x < 10:
product = product * x
x = x + 1
print(sum, product)
#Output 45, 10-->this is correct
#In the IDLE
def sum():
x = 1
sum = 0
while x < 10:
sum = sum + x
x = x + 1
def product():
product = x
while x < 10:
product = product * x
x = x + 1
print(sum, product)
#Code Fix #3
#Original Code
def print_range(start, end):
#loop through the numbers from start to end
n = start
while n <=end:
print (n)
print_range (1,5)
#Output should be 1 2 3 4 5 each number on its own line
#Correct Code
def print_range(start, end):
#loop through the numbers from start to end
n = start
while n <=end:
print (n)
n=n+1 #or n+=1# #Without incrementing through n, this will always be true-->must remember to increment through an iterative
print_range (1,5)
#Code Fix #4
#Original Code
def is_power_of_two(number):
while number % 2 == 0:
number = number/2
if number == 1:
return True
return False
#Output should be
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False
#Correct Code
def is_power_of_two(number):
while number % 2 == 0 and number != 0: # issue is ZeroDivisionError-->must set the while statment to not equal 0
number = number/2
if number == 1:
return True
return False
#Output should be
print(is_power_of_two(0)) # Should be False
print(is_power_of_two(1)) # Should be True
print(is_power_of_two(8)) # Should be True
print(is_power_of_two(9)) # Should be False
#Code Completion #1
"The function accepts a variable “number” through its parameters."
"This “number” variable should be multiplied by the numbers 1 through 5,"
"and printed in a format similar to “1x6=6” (“number” x “multiplier” = “result”)."
"The code should also limit the “result” to not exceed 25."
#Original Code
def multiplication_table(number):
# Initialize the appropriate variable
_=_
# Complete the while loop condition.
while _:
result = number * multiplier
if result > 25:
# Enter the action to take if the result > 25
_
print(str(number) + "x" + str(multiplier) + "=" + str(result))
# Increment the appropriate variable
_ += 1
#Output
multiplication_table(3)
# Should print:
# 3x1=3
# 3x2=6
# 3x3=9
# 3x4=12
# 3x5=15
multiplication_table(5)
# Should print:
# 5x1=5
# 5x2=10
# 5x3=15
# 5x4=20
# 5x5=25
multiplication_table(8)
# Should print:
# 8x1=8
# 8x2=16
# 8x3=24
#Completed Code
def multiplication_table(number):
# Initialize the appropriate variable
multiplier = 1
# Complete the while loop condition.
while multiplier <=5: #use this to ensure that the code runs only when the multiplier is less than or equal to 5
result = number * multiplier
if result > 25:
# Enter the action to take if the result > 25
break
print(str(number) + "x" + str(multiplier) + "=" + str(result))
# Increment the appropriate variable
multiplier += 1 #incrementing the multplier so that you it only takes numbers 1-5
#Output
multiplication_table(3)
# Should print:
# 3x1=3
# 3x2=6
# 3x3=9
# 3x4=12
# 3x5=15
multiplication_table(5)
# Should print:
# 5x1=5
# 5x2=10
# 5x3=15
# 5x4=20
# 5x5=25
multiplication_table(8)
# Should print:
# 8x1=8
# 8x2=16
# 8x3=24