For loops (2) Flashcards
Print “Hello” ten times.
for i in range(10):
print(“Hello”)
Ask the user for 3 numbers. Write a program that calculates the square of the numbers.
for i in range(3):
num=eval(input(“Write a number: “))
print(“The square of the number is: “, num*num)
Write a program that displays the following:
A
B
C
D
C
D
C
D
C
D
E
print(‘A’)
print(‘B’)
for i in range(4):
print(‘C’)
print(‘D’)
print(‘E’)
Write a program that displays the following:
A
B
C
C
C
C
C
D
D
D
D
E
print(‘A’)
print(‘B’)
for i in range(5):
print(‘C’)
for i in range(4):
print(‘D’)
print(‘E’)
Write a program that displays the following:
A
B
CCCCC
DDDD
E
print(‘A’)
print(‘B’)
for i in loop(5):
print(‘C’, end=’’)
print()
for i in loop(4):
print(‘D’, end=’’)
print()
print(‘E’)
Write a program that displays the numbers from 1 to 100. It should start with 1, not with 0.
for i in loop(100):
print(i+1)
Write a program that displays the following:
1—Hello
2—Hello
3—Hello
4—Hello
.
.
.
50—Hello
for i in range(50):
print(i+1, ‘— Hello’)
Write a program that displays the following:
5 4 3 2 1 Blast off!
for i in range(5,0,-1):
print(i, end=’’)
print(“Balst off!”)
Write a program that displays the following:
⁎⁎⁎⁎⁎⁎
⁎⁎⁎⁎⁎⁎
⁎⁎⁎⁎⁎⁎
⁎⁎⁎⁎⁎⁎
for i in range(4):
print(‘⁎’⁎6)
Write a program that displays the following:
⁎
⁎⁎
⁎⁎⁎
⁎⁎⁎⁎
⁎⁎⁎⁎⁎
for i in range(5):
print(‘⁎’⁎(i+1))
Write a program that writes your name 100 times.
for i in range(100):
print(‘Simona’)
Write a program to fill the screen horizontally and vertically with your name.
for i in range(100):
print(i+1, “Simona”⁎50, end=’\n’)
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
for i in loop(100):
print(i+1, “Simona”)
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
for i in range(1,21):
print(i, “—”, i⁎⁎2)
Write a program that uses a for loop to print the numbers: 8, 11, 14, 17, 20, …, 83, 86, 89.
for i in range(8, 90, 3):
print(i, “ ,”, end=’’)
Write a program that uses a for loop to print the numbers: 100, 98, 96, …, 4, 2.
for i in range(100, 0, -2):
print(i, “ “, end=’’
Write a program that uses exactly four for loops to print the sequence of letters below:
AAAAAAAAAABBBBBBBCDCDCDCDEFFFFFFG
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’)
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.
name=input(“Tell me your name: “)
num=int(input(“Tell me a number: “))
for i in range(num):
print(name)
Write a program that asks the user how many Fibonacci numbers to print and then prints that many.
number=int(input(“How many numbers? “))
nums=[0,1]
for i in range(2, number):
nums.append(nums[-1]+nums[-2])
print(nums)
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.
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)
Use a for loop to print a box like the one below. Allow the user to specify how high the triangle should be.
⁎
⁎⁎
⁎⁎⁎
⁎⁎⁎⁎
height=int(input(“How tall? “))
for i in range (height):
print(‘⁎’ ⁎(i+1))
Use a for loop to print a box like the one below. Allow the user to specify how high the triangle should be.
⁎⁎⁎⁎
⁎⁎⁎
⁎⁎
⁎
height=int(input(“How tall is the triangle? “))
for i in range(height,0,-1):
print(‘⁎’ ⁎ i)