Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I had indented the code. And I have fixed the integer to string. But, still the code doesn't work.

+3 votes
asked Nov 19, 2020 by npatel300 (1,440 points)
name1_front = "Charles"

student_name = "charles", "Chelsea", "Mary"

name3_end = "Mary"

num_students = 0

string = student_name

def main():

num_students = int(input('how many students in class:'))

print('Number of students in class are', num_students)

#open the names.txt file for reading

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

for count in range(1, num_students + 1):

student_name = string(input('enter the name of student:'))

#read the names to file

name_file.read(student_name)

#close the File

name_file.close()

print('Data read to names.txt.')

print('The student that would be front of the line is', name1_front)

print('The student that would be end of the line is', name3_end)

#call the main function

main()

1 Answer

0 votes
answered Nov 19, 2020 by xDELLx (10,500 points)

Not sure if you want to read data from file or write to it. I may be wrong but Going by the "logic structure" in code I guess is you want to collect names of students & dump them to a file.
But the code writeen does the opposite & reads from file.
Anyway I have got the below code working , provided there is a file "student_names.txt" present in current dir & has some data available.

Based on this you can alter it to write names .


student_name = "charles", "Chelsea", "Mary"
name3_end = "Mary"
num_students = 0
string = student_name

def main():
    num_students = int(input('how many students in class:'))
    print('Number of students in class are', num_students)

    #open the names.txt file for reading
    name_file = open('student_names.txt', 'r')
    for count in range(1, num_students + 1):

        student_name = str(input('enter the name of student:'))

        #read the names to file
        fileData = str (name_file.read())
        print('Data read from names.txt.', fileData )

    #close the File
    name_file.close()

    print('Data read to names.txt.')
    print('The student that would be front of the line is', name1_front)
    print('The student that would be end of the line is', name3_end)

#call the main function
main()



commented Nov 19, 2020 by npatel300 (1,440 points)
I am still getting an error of  file not found and no such file or directory: 'names.txt'. How should I fix?
commented Nov 19, 2020 by xDELLx (10,500 points)
the code shared above uses  student_names.txt to read.
----> name_file = open('student_names.txt', 'r')

If you are using names.txt in your code , create a file with name "names.txt" & try to execute code.

Also are you sure about the data reading from file??
The code structure is more inclined to writing data to file you should be writing data to file
commented Nov 20, 2020 by npatel300 (1,440 points)
So, how do I create a file with name "names.txt"?
Yes, I have to the data reading from file, not writing.
commented Nov 20, 2020 by xDELLx (10,500 points)
edited Nov 20, 2020 by xDELLx
On Windows , right clivk & create a new text file .
On Linux (in terminal)  echo String >> filename.txt

----------------------------------------------------------------------------------------------------------------------------
Below is what I did on Linux terminal:

mint@mint:~$ #Dump Some randon names to student_names.txt
mint@mint:~$ #                                                          ------------------------------
mint@mint:~$   echo -e "ABCD\nQWERTY\nASDF\nZXCV\nERTDFXCV\n" >> student_names.txt

mint@mint:~$
mint@mint:~$ #Check contents of file student_names.txt
mint@mint:~$ cat student_names.txt
ABCD
QWERTY
ASDF
ZXCV
ERTDFXCV
mint@mint:~$ #Executing the python script
mint@mint:~$ python3 x.py
how many students in class:1
Number of students in class are 1
enter the name of student:asd
Data read from names.txt. ABCD
QWERTY
ASDF
ZXCV
ERTDFXCV


Data read to names.txt.
The student that would be front of the line is Charles
The student that would be end of the line is Mary
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
But
Few things to consider first:
Does your requirement state that the file needs to exist & only then can the code execute.
Or does it the requirement state the file will be created on the fly ,if no  file exits .
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...