Part 1 Exercise Answers: X Input ("Enter Your Name:") For I in Range (1,101) : Print (X, End ' )

You might also like

You are on page 1of 4

PART 1 EXERCISE ANSWERS

1. Write a program that prints your name 100 times.

x=input("enter your name:")

for i in range(1,101):

print(x,end=’ ‘)

2. Write a program to fill the screen horizontally and vertically with your name.
[Hint: add the option end='' into the print function to fill the screen horizontally.]

3. Write a program that outputs 100 lines, numbered 1 to 100, each with your name
on it.

x=input("enter your name:")

for i in range(1,101):

print(i,x)

4. Write a program that prints out a list of the integers from 1 to 20 and their
squares.
for i in range(1,21):
print(i,i**2)

5. W r i t e a p r o g r a m t h
86, 89.
for i in range(8,90,3):

print(i,end=' ')

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

for i in range(100,1,-2):

print(i,end=' ')

7. 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('CD' ,end='')
print('E',end='')
for i in range(6):
print('F',end='')
print('G',end='')

8. 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.

x=int(input("enter no of times:"))
a=input("enter name:")
for i in range (x):
print(a)
9. The Fibonacci numbers are the sequence below, where the first two numbers are
1, and each number thereafter is the sum of the two preceding numbers. Write a
program that asks the user how many Fibonacci numbers to print and then prints
that many.
1,1,2,3,5,8,13,21,34,55,89...

a=int(input("Enter the first number of the series "))


b=int(input("Enter the second number of the series "))
n=int(input("Enter the number of terms needed "))
print(a,b,end=" ")
while(n-2!=0):
c=a+b
a=b
b=c
print(c,end=" ")
n=n-1

10. 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. [Hint: print('*'*10) prints ten asterisks.]
*******************
*******************
*******************
*******************

for i in range (4):


print('*'*10)

11. Write a program that asks the user to enter a word that contains the letter a. The program
should then print the following two lines: On the first line should be the part of the string up to and
including the the first a, and on the second line should be the rest of the string. Sample output is
shown below:
Enter a word: buffalo buffa lo

x=input("enter string with a")


l=len(x)
for i in range (l):
if x[i]=='a' or x[i]=='A':
print(x)
print(x[0:i+1])
print(x[i+1:-1])

12. Write a program that asks the user to enter a word and then capitalizes every other letter of that
word. So if the user enters rhinoceros, the program should print rHiNoCeRoS.

str=input("Enter a string:")

l=len(str)
print("original string:", str)
str2=''
for a in range (0,l):
if a%2==0:
str2 += str[a]
else:
str2+=str[a].upper()
print(str2)

You might also like