169k views
2 votes
Write a loop that displays your name 10 times. 2. Write a loop that displays all the odd numbers from 1 through 49. 3. Write a loop that displays every fifth number from 0 through 100. 4. Write a code sample that uses a loop to write the numbers from 1 through 10 to a file. 5. Assume that a file named People.txt contains a list of names. Write a code sample that uses a while loop to

1 Answer

5 votes

Answer:

The program in Python is as follows:

#1

for i in range(10):

print("MrRoyal",end=" ")

print()

#2

for i in range(1,50,2):

print(i,end=" ")

print()

#3

for i in range(0,101,5):

print(i,end=" ")

print()

#4

i = 1

while i <=10:

print(i,end =" ")

i+=1

#5

myfile = open("People.txt", "r")

eachLine = myfile.readline()

while eachLine:

print(eachLine)

eachLine = myfile.readline()

myfile.close()

Step-by-step explanation:

The program in Python is as follows:

Program #1

This iterates from 1 to 10

for i in range(10):

This prints the name for each iteration [modify to your name]

print("MrRoyal",end=" ")

This prints a new line

print()

Program #2

This iterates from 1 to 49 with an increment of 2

for i in range(1,50,2):

This prints the odd numbers in the above range

print(i,end=" ")

This prints a new line

print()

Program #3

This iterates from 0 to 100 with a step of 5

for i in range(0,101,5):

This prints every 5th number

print(i,end=" ")

Print a new line

print()

Program #4

This initializes the number to 1

i = 1

This opens the file in an append mode

f = open("myfile.txt", "a")

This loop is repeated from 1 to 10

while i <=10:

This writes each number to the file

f.write(str(i))

Increment the number by 1

i+=1

Close the file

f.close()

Program #5

This opens the file in a read mode

myfile = open("People.txt", "r")

This reads each line

eachLine = myfile.readline()

This loop is repeated for every line

while eachLine:

Print the content on the line

print(eachLine)

Read another line

eachLine = myfile.readline()

Close the file

myfile.close()

User Igal Zeifman
by
5.3k points