For loops (2) Flashcards

1
Q

Print “Hello” ten times.

A

for i in range(10):
print(“Hello”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Ask the user for 3 numbers. Write a program that calculates the square of the numbers.

A

for i in range(3):
num=eval(input(“Write a number: “))
print(“The square of the number is: “, num*num)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a program that displays the following:
A
B
C
D
C
D
C
D
C
D
E

A

print(‘A’)
print(‘B’)
for i in range(4):
print(‘C’)
print(‘D’)
print(‘E’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a program that displays the following:
A
B
C
C
C
C
C
D
D
D
D
E

A

print(‘A’)
print(‘B’)
for i in range(5):
print(‘C’)
for i in range(4):
print(‘D’)
print(‘E’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a program that displays the following:
A
B
CCCCC
DDDD
E

A

print(‘A’)
print(‘B’)
for i in loop(5):
print(‘C’, end=’’)
print()
for i in loop(4):
print(‘D’, end=’’)
print()
print(‘E’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a program that displays the numbers from 1 to 100. It should start with 1, not with 0.

A

for i in loop(100):
print(i+1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a program that displays the following:
1—Hello
2—Hello
3—Hello
4—Hello
.
.
.
50—Hello

A

for i in range(50):
print(i+1, ‘— Hello’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a program that displays the following:
5 4 3 2 1 Blast off!

A

for i in range(5,0,-1):
print(i, end=’’)
print(“Balst off!”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a program that displays the following:
⁎⁎⁎⁎⁎⁎
⁎⁎⁎⁎⁎⁎
⁎⁎⁎⁎⁎⁎
⁎⁎⁎⁎⁎⁎

A

for i in range(4):
print(‘⁎’⁎6)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a program that displays the following:

⁎⁎
⁎⁎⁎
⁎⁎⁎⁎
⁎⁎⁎⁎⁎

A

for i in range(5):
print(‘⁎’⁎(i+1))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a program that writes your name 100 times.

A

for i in range(100):
print(‘Simona’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a program to fill the screen horizontally and vertically with your name.

A

for i in range(100):
print(i+1, “Simona”⁎50, end=’\n’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a program that outputs 100 lines, numbered 1 to 100, each with your name on it. The output should look like this:
1 Simona
2 Simona
3 Simona
.
.
.
100 Simona

A

for i in loop(100):
print(i+1, “Simona”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a program that prints out a list of integers from 1 to 20 and their squares. The output should look like this:
1 — 1
2 — 4
3 — 9
.
.
.
20 — 400

A

for i in range(1,21):
print(i, “—”, i⁎⁎2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a program that uses a for loop to print the numbers: 8, 11, 14, 17, 20, …, 83, 86, 89.

A

for i in range(8, 90, 3):
print(i, “ ,”, end=’’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write a program that uses a for loop to print the numbers: 100, 98, 96, …, 4, 2.

A

for i in range(100, 0, -2):
print(i, “ “, end=’’

17
Q

Write a program that uses exactly four for loops to print the sequence of letters below:
AAAAAAAAAABBBBBBBCDCDCDCDEFFFFFFG

A

for i in range(10):
print(‘A’, end=’’)
for i in range(7):
print(‘B’, end=’’)
for i in range(4):
print(‘C’, end=’’)
print(‘D’, end=’’)
print(‘E’, end=’’)
for i in range(6):
print(‘F’, end=’’)
print(‘G’)

18
Q

Write a program that asks the user for their name and how many times to print it. The program should print out the user’s name the specified number of times.

A

name=input(“Tell me your name: “)
num=int(input(“Tell me a number: “))
for i in range(num):
print(name)

19
Q

Write a program that asks the user how many Fibonacci numbers to print and then prints that many.

A

number=int(input(“How many numbers? “))
nums=[0,1]
for i in range(2, number):
nums.append(nums[-1]+nums[-2])
print(nums)

20
Q

Use a for loop to print a box like the one below. Allow the user to specify how wide and how high the box should be.

A

wide=int(input(“How wide should the box be? “))
high=int(input(“How high should the box be? “))
for i in range(wide):
print(‘⁎’⁎high)

21
Q

Use a for loop to print a box like the one below. Allow the user to specify how high the triangle should be.

⁎⁎
⁎⁎⁎
⁎⁎⁎⁎

A

height=int(input(“How tall? “))
for i in range (height):
print(‘⁎’ ⁎(i+1))

22
Q

Use a for loop to print a box like the one below. Allow the user to specify how high the triangle should be.
⁎⁎⁎⁎
⁎⁎⁎
⁎⁎

A

height=int(input(“How tall is the triangle? “))
for i in range(height,0,-1):
print(‘⁎’ ⁎ i)