Insert a file in a for loop

Dyl07 Posted messages 3 Status Membre -  
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   -

How are you? I hope you're doing well.

Please, I need help.

By the way, I want to insert a file in a for loop so that as the loop executes, the output information gets collected in the file, somewhat like when we have a classroom and want to collect students' grades using a for loop. When we get to the first student, their grades should be collected in a file, and then we move on to the second student... but I'm struggling with this. Do you perhaps have an idea of how I can get through this? Please.

3 réponses

choubaka Posted messages 5529 Registration date   Status Modérateur Last intervention   2 113
 

Hello

Well, show us the code you've come up with so we can help you.


0
Dyl07 Posted messages 3 Status Membre
 

for number in range(1, number):

print("\n Enter the student's name")

ch = input()

name = str(ch)

print("\n Enter the first grade")

ch = input()

grade1 = float(ch)

print("\n Enter the second grade")

ch = input()

grade2 = float(ch)

la = []

la.append(name)

la.append(grade1)

la.append(grade2)

fic = open("Bulletin.txt", "a")

fic.write(str(la) + "\n")

fic.close()

Here's a bit of the part that bothers me, actually everything works for the first student and their grades end up in the file named bulletin.txt, but after the first student, the code stops.

0
yg_be Posted messages 23437 Registration date   Status Contributeur Last intervention   1 587
 

Hello,

rather:

for counter in range(1, number): 
0
doudou
 

Hello,

Your file is not opened in write mode, so no records can be made.

Additionally, either you arrange to open your file only once in write mode (before the loop), and write the data from your loop, or you open it in append mode; otherwise, you will overwrite the previous data on each iteration of your loop.

Otherwise, why does your range start at 1?

0
jordane45 Posted messages 30426 Registration date   Status Modérateur Last intervention   4 830
 

Hello,

In addition to the answers already given, sometimes (often), reading tutorials or documentation will give you a good foundation to address the issues you are facing.

Starting with:

https://www.w3schools.com/python/python_file_write.asp

https://www.google.com/search?q=python+ecrire+dans+un+fichier

PS: in the future, when posting code on the forum, please use the icon provided for this purpose in the bar above the area where you write your message...

This will make your code more readable... and especially maintain the code's indentation (essential in Python) and syntax highlighting...

Thank you.


.
Best regards,
Jordane

0