106k views
5 votes
Can someone explain to me how to use the while loop for this exercise?

Can someone explain to me how to use the while loop for this exercise?-example-1

2 Answers

7 votes

Answer:

To use a while loop for this exercise, you would first initialize a variable, such as "counter," to 0. Then, you would set up the while loop so that it continues to run as long as the value of "counter" is less than the total number of sections you have in your data set. Within the while loop, you would include instructions to read in the data for each section, such as using the input() function to prompt the user for the course number, section, and meeting time and location. You would then use an if statement to check if the course is an online course, and if so, you would print "online" in place of the meeting time and location. Finally, you would increment the "counter" variable by 1 at the end of each iteration of the while loop so that the loop eventually terminates when all sections have been read in and printed.

User Notilas
by
7.0k points
2 votes

Answer:

Step-by-step explanation:

There is insufficient information to accurately respond but I have done in a simple manner.

Since I don't know what the contents of the two python files referred to in the document, I have used my own formatting method and check for moving to a newline.

Also, don't know how the data is inputted; I have assumed that the data is in a file called data.txt

There are more elegant ways t do this but I have done this quickly and simply

with open("data.txt") as f: # open file for reading

data = f.read() # read all data

lines = data.split() #split data into individuals

i = 1 #counter to track how many lines outputted

for line in lines: # for each item in lines

print('{:20s}'.format(line), end = '') # suppress line feed

if i % 3 == 0: #check to see if 3 items printed out
print() # if so, print new line

i+=1 #increment counter

f.close() # close file - not necessary if using open

User Yaro
by
6.7k points