40.7k views
15 votes
Python exercise c2 q4

Sam wants to write a code that will produce the following:
000
001
010
011
100
101
110
111

He doesn’t know where to start so he starts with the basic:
00
01
10
11

However, he also has trouble finishing this code. He only has a basic idea. Help him to complete the basic version first:

for i in range (0, 2):
for j in range (0, ): (1 mark)
print( i , ) (1 mark)



Now that you know the basic, complete the code that will print the chart 000, 001….all the way to 111 (Hint: look at the code you just complete. This time you probability need 3 for loops, one within another)

for i in range (0, 2):
for j in range ( , ): (2 mark)
for k in range ( , ): (2 marks)
print( , , k ) (1 mark)

User RED MONKEY
by
3.5k points

1 Answer

7 votes

Answer:

for i in range (0, 2):

for j in range (0, 2):

print(i , j)

and

for i in range (0, 2):

for j in range (0, 2):

for k in range (0, 2):

print(i , j, k)

Step-by-step explanation:

The nicer method of doing this would be:

for i in range (0, 8):

print("{0:03b}".format(i))

User Soroush Hakami
by
3.8k points