Ini yang coba saya tampilkan. Ini adalah produk sampingan dari 2 fungsi. Paragraf pertama adalah fungsi create() yang membuat file "grades.txt". Saya pikir saya turun cukup mudah.

Kedua, paragraf selanjutnya adalah dari fungsi "Retrieve()" membaca dari file "grades.txt" dan menampilkan nama kelas dan IPK kemudian menghitung rata-rata untuk kelas tersebut dan menampilkannya. Saya merasa seperti berada di titik puncak tetapi saya terjebak ke mana harus pergi dari sini karena saya terganggu dengan kesalahan "STRG".

 >     def main ():
    >         create()
    >         retrieve()
    >         
    >     def create():
    >             outfile = open('grades.txt', 'w')
    >             count_files = 0
    >     
    >             #Gather class name or press 'Enter' to quit
    >             class_or_exit = input('Enter course name or Enter to quit: ')
    >     
    >             #Create While loop if 'Enter' is pressed exit and close file
    >             while class_or_exit !="":
    >                 count_files += 1
    >                 grade =(input('Enter grade (interger) achieved: '))
    >                 class_or_exit = input('Enter course name or Enter to quit: ')
    >                 
    >         
    >     
    >             #Write info to the file
    >             outfile.write(str(class_or_exit) + '\n')
    >             outfile.write(str(grade) + '\n')
    >     
    >             #Close file
    >             outfile.close()
    >             print('File was created and closed')
    >             return grade, class_or_exit
    >         
    >     def retrieve():
    >         print('Here is your GPA for the classes you entered:')
    >         outfile = open('grades.txt', 'r')
    >         total = 0.0
    >         count = 0
    >         class_or_exit = outfile.readline()
    >         grade = float(outfile.readline())
    >         
    >         if grade >= 90:
    >             grade = 4.0
    >             count = count + 1
    >             print(f"{class_or_exit:} class" + str + "{grade:.2f}")
    >         elif grade >= 80:
    >             grade = 3.0
    >             count = count + 1
    >             print(f'{class_or_exit:} class'+ str + "{grade:.2f}")
    >          elif grade >= 80:
    >             grade = 2.0
    >             count = count + 1
    >             print(f'{class_or_exit:} class'+ str + "{grade:.2f}")
    >          elif grade >= 80:
    >             grade = 1.0
    >             count = count + 1
    >             print(f'{class_or_exit:} class'+ str + "{grade:.2f}")
    >         
    >     main()
0
Joshua Chung 5 April 2021, 08:55

1 menjawab

Jawaban Terbaik

Ini melakukan apa yang saya pikir Anda ingin lakukan. Perhatikan baik-baik perbedaan antara ini dan apa yang Anda tulis.

def create():
    outfile = open('grades.txt', 'w')

    #Gather class name or press 'Enter' to quit
    class_or_exit = input('Enter course name or Enter to quit: ')

    #Create While loop if 'Enter' is pressed exit and close file
    while class_or_exit !="":
        grade =(input('Enter grade (interger) achieved: '))
        #Write info to the file
        outfile.write(class_or_exit + '\n')
        outfile.write(grade + '\n')
        class_or_exit = input('Enter course name or Enter to quit: ')

    #Close file
    outfile.close()
    print('File was created and closed')
    return grade, class_or_exit
    
def retrieve():
    print('Here is your GPA for the classes you entered:')
    outfile = open('grades.txt', 'r')
    total = 0.0
    count = 0
    while True:
        class_or_exit = outfile.readline()
        if not class_or_exit:
            break
        grade = float(outfile.readline())
        count += 1
    
        if grade >= 90:
            total += 4.0
        elif grade >= 80:
            total += 3.0
        elif grade >= 70:
            total += 2.0
        elif grade >= 60:
            total += 1.0

    print("Your GPA is", total/count)

def main ():
    create()
    retrieve()
    
main()
0
Tim Roberts 5 April 2021, 06:09