I am trying to write a program that 1. Lets the user write new students and there overall grade to the file, 2. Opens the file to view course info(student and their grade) 3. Shows course stats(mean and range for the class). Here is what I have so far, can someone help me out...I'm stuck and don't know where to go from here.

def main():
    filename1 = input("What is the file name?")

    file1 = open(filename1, "a")
    file1.close()

    choice = input("Please enter the number for your choice or press <enter> to quit\n\n1.) Add a student to File\n2.) Display course info\n3.) Display course stats.")
    if choice == '1':
     studentName = input("What is the student's name?")
    elif choice == '2':
     function_2()#should open file to read file
    elif choice == '3':
     function_3()#display results from arithmetic mean and range for the course

    file1 = open(filename1,"a")
    file1.write(myInfo + ","   ",\n" )
    file1.close()





if __name__ == '__main__':
    main()

Recommended Answers

All 6 Replies

I do not understand line 16 (myInfo does not exist and s trange strings syntax) or lines 4 and 5 (as you are not catching exceptions)

Open and read the file once into a list at the start of the program. If there are adds, simply add them to the list. Write the file once at the end of the program. I am guessing that each record in the file will contain the student's name and the grade so you can satisfy the requirements for #3. Start by getting the name and grade many times and appending each to a list, until the user enters "quit" for the student name, then write the file to disk and verify it's contents. The other parts have to wait until this is finished.

Open and read the file once into a list at the start of the program. If there are adds, simply add them to the list. Write the file once at the end of the program. I am guessing that each record in the file will contain the student's name and the grade so you can satisfy the requirements for #3. Start by getting the name and grade many times and appending each to a list, until the user enters "quit" for the student name, then write the file to disk and verify it's contents. The other parts have to wait until this is finished.

Thank you, Woooee!

I am having trouble writing that to code, truthfully I don't know if my code is even right(I am very new to this). I'm not asking you or anyone to do it for me, I just need some help writing the code. For example, when I prompt the user and say the user presses #2 how do I write the code to get it to go and read file is that with the if and elif? what does it look like in code. I hope you can understand my question.

See this link on functions http://www.tutorialspoint.com/python/python_functions.htm You should also have a function_1 that will input the name and grade until told to stop. It would also receive the "data" list and append to it. If the length of "data" is zero, then option 2 has not been selected. You can either print an error message or call function_2 from function_1 to read the data. It is a good habit to include some useful info about what the function does with every function call for future reference.

def function_2():
    """ get the file name, open and read the file, and return the data
    """
    fname = input("What is the file name?")
 
    file1 = open(fname, "r")
    data = file1.readlines()
    file1.close()
    return data

def function_3(data):
    """ process the data by calculating the mean
    """
    for rec in data:
        print rec
 

def main():
    data=""
    choice = input("Please enter the number for your choice or press <enter> to quit\n\n1.) Add a student to File\n2.) Display course info\n3.) Display course stats.")
    if choice == '1':
        function_1(data)
    elif choice == '2':
     data=function_2() #should open file to read file
     print data
    elif choice == '3':
     function_3(data) #display results from arithmetic mean and range for the course
 
    else:
        print "Enter 1, 2, or 3 only"

    file1 = open(filename1,"a")
    file1.write(myInfo + ","   ",\n" )
    file1.close()
 
 
 
 
 
if __name__ == '__main__':
    main()

See this link on functions http://www.tutorialspoint.com/python/python_functions.htm You should also have a function_1 that will input the name and grade until told to stop. It would also receive the "data" list and append to it. If the length of "data" is zero, then option 2 has not been selected. You can either print an error message or call function_2 from function_1 to read the data. It is a good habit to include some useful info about what the function does with every function call for future reference.

def function_2():
    """ get the file name, open and read the file, and return the data
    """
    fname = input("What is the file name?")
 
    file1 = open(fname, "r")
    data = file1.readlines()
    file1.close()
    return data

def function_3(data):
    """ process the data by calculating the mean
    """
    for rec in data:
        print rec
 

def main():
    data=""
    choice = input("Please enter the number for your choice or press <enter> to quit\n\n1.) Add a student to File\n2.) Display course info\n3.) Display course stats.")
    if choice == '1':
     studentName = input("What is the student's name?")
    elif choice == '2':
     data=function_2() #should open file to read file
     print data
    elif choice == '3':
     function_3(data) #display results from arithmetic mean and range for the course
 
    file1 = open(filename1,"a")
    file1.write(myInfo + ","   ",\n" )
    file1.close()
 
 
 
 
 
if __name__ == '__main__':
    main()

Thanks again!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.