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 by npatel300 (300 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 by xDELLx (4,800 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 by npatel300 (300 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 by xDELLx (4,800 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 6 days ago by npatel300 (300 points)
So, how do I create a file with name "names.txt"?
Yes, I have to the data reading from file, not writing.
commented 6 days ago by xDELLx (4,800 points)
edited 6 days ago 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:

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

[email protected]:~$
[email protected]:~$ #Check contents of file student_names.txt
[email protected]:~$ cat student_names.txt
ABCD
QWERTY
ASDF
ZXCV
ERTDFXCV
[email protected]:~$ #Executing the python script
[email protected]:~$ 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.
...