For Loops and range() – Checklist

For every item on the list, write a Python program that creates the exact output shown in the box.

In each case, you will need to use a for loop and the range() function.

1) Print “Dragons eat tacos.” five times using only two lines of code.

Output:

Dragons eat tacos.
Dragons eat tacos.
Dragons eat tacos.
Dragons eat tacos.
Dragons eat tacos.

2) Print your counter variable to  print each number from 0 to 8 using two lines of code.

Output:

0
1
2
3
4
5
6
7
8

3) Print some text along with the numbers.

Output:

This is line 0
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8

4) Count by 3’s starting at 6 and print “Finished!” at the end.

Output:

6
9
12
15
18
21
Finished!

5) Count backwards from 15 to 8 using range()

Output:

15
14
13
12
11
10
9
8

6) Math: print the cube of every number in range(9). Use ** operator. Print each line as a string as shown below.

Output:

0 cubed is 0
1 cubed is 1
2 cubed is 8
3 cubed is 27
4 cubed is 64
5 cubed is 125
6 cubed is 216
7 cubed is 343
8 cubed is 512

7) Print every positive integer under 200 that is divisible by 7. Use the modulus operator, %, to check if the remainder is equal to zero: “x % 7 == 0” (can be done in 3 lines of code)

Output:

7 is divisible by 7
14 is divisible by 7
21 is divisible by 7
28 is divisible by 7
35 is divisible by 7
42 is divisible by 7
49 is divisible by 7
56 is divisible by 7
63 is divisible by 7
70 is divisible by 7
77 is divisible by 7
84 is divisible by 7
91 is divisible by 7
98 is divisible by 7
105 is divisible by 7
112 is divisible by 7
119 is divisible by 7
126 is divisible by 7
133 is divisible by 7
140 is divisible by 7
147 is divisible by 7
154 is divisible by 7
161 is divisible by 7
168 is divisible by 7
175 is divisible by 7
182 is divisible by 7
189 is divisible by 7
196 is divisible by 7

8) Print an increasing number of dots up to 14 dots. Write 3 lines of code or less.

Output:

.
..
...
....
.....
......
.......
........
.........
..........
...........
............
.............
..............

9) Ask the user for a number of kilometers. Multiply this number by 1000 to convert it to meters. Repeat this process four times.  Hint: you will need the int() or float() function. (Can be done in 3 lines of code)

Enter number of km: 5
5 km is 5000 m.
Enter number of km: 12
12 km is 12000 m.
Enter number of km: 3
3 km is 3000 m.
Enter number of km: 1
1 km is 1000 m.

10) Three tries: ask a user to guess a secret number from 1 to 10. If they get it right, use the break command to quit the loop and print a win message. If they get it wrong three times, print a game over message. (Can be done in 7 lines of code, but it’s okay if it’s a bit longer than that)

Output with wrong guesses:

Pick a number from 1 to 10: 2
Pick a number from 1 to 10: 3
Pick a number from 1 to 10: 4
GAME OVER

Output with a correct guess on try #1:

Pick a number from 1 to 10: 9
YOU WIN