214k views
3 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 read the file and display its contents in a ListBox control.

User Nullable
by
5.0k points

1 Answer

1 vote

Answer:

Kindly check explanation

Step-by-step explanation:

(1.)

n = 0

While n < 10 :

n+=1

print('fichoh')

# initiate counter n = 0 ; set condition, while n is not equal to 10 ; it print 'fichoh' and counter is increased by 1. This terminates once it reaches 10.

(2.)

for number in range(1,50):

if number%2 != 0 :

print(number)

#An odd number won't have a remainder of 0 when Divided by 2 ; so, for numbers 1 to 49 using the range function, if the number is divided by 2 and leaves a remainder other Than 0, such number is displayed

(3.)

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

print(number)

# Using the range function, starting from 0 to 100; it is number in the for loop is incremented by 5 and displayed.

(4.)

outfile = open('my_file.txt', 'w')

for n in range(0,11):

outfile.write(str(n))

outfile.close()

#open, 'w' creates a text file, my_text.txt and writes into it ; using the range function, numbers 0 to 10 is written into the created file.

5.)

name_file = open('People.txt', 'r')

While name_file:

out_file = name_file.readline()

if out_file = ' ' :

break

name_file.close()

names = name

#Open, 'r' ; opens and read from the file People.txt ;

#readline reads the content of the opened text file into a list out_file ; using the if ' ', the loop terminates once an empty content is read.

User Khaled Ayed
by
4.4k points