Python Forum
Help with displaying output of a python program?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with displaying output of a python program?
#1
I'm doing an assignment that he wants us to display the output of the file when we run it. (We're working on reading and writing files in Python and such.) Here's my code:

def determine_grade(): # Define a function.
    asgnts_dict = dict(sec1=50, sec2=50, sec3=50, sec4=50, sec5=50,
                       sec6=50, sec7=50, sec8=50, sec9=50, sec10=50) # Create dictionary of values.
    asgnts_list = list(asgnts_dict.values()) # Take values from dictionary, and convert it into a list.
    print('List of grades:\n'
          'Section 1: %s\n'
          'Section 2: %s\n'
          'Section 3: %s\n'
          'Section 4: %s\n'
          'Section 5: %s\n'
          'Section 6: %s\n'
          'Section 7: %s\n'
          'Section 8: %s\n'
          'Section 9: %s\n'
          'Section 10: %s\n'
          % (asgnts_list[0], asgnts_list[1], asgnts_list[2], asgnts_list[3], asgnts_list[4],
             asgnts_list[5], asgnts_list[6], asgnts_list[7], asgnts_list[8], asgnts_list[9],))
        # Print out the grades section by section.

    full_points = sum(asgnts_list) # Sum all numbers in list, assign it as one full number.
    print('Total amount of points in course: %d' % full_points)
    student_grd_dict = dict(sec1=50, sec2=50, sec3=50, sec4=50) # Create dictionary for student grades.
    student_grd_list = list(student_grd_dict.values()) # Convert into list of numbers.
    student_grd_list[0] = 45
    student_grd_list[1] = 25
    student_grd_list[2] = 40 # Change values of list.
    print('List of grades:\n'
          'Section 1: %s\n'
          'Section 2: %s\n'
          'Section 3: %s\n'
          'Section 4: %s\n'
          'Section 5: --\n'
          'Section 6: --\n'
          'Section 7: --\n'
          'Section 8: --\n'
          'Section 9: --\n'
          'Section 10: --\n'
          % (student_grd_list[0], student_grd_list[1], student_grd_list[2], student_grd_list[3]))
    # Print out values of student list.

    full_student_pts = sum(student_grd_list) # Calculate the sum of the grades for student.
    print('Total amount of points in course: %d' % full_student_pts)

    possible_grade = (full_student_pts + (asgnts_list[4] + asgnts_list[5] + asgnts_list[6] + asgnts_list[7] +
                                          asgnts_list[8] + asgnts_list[9]))
    # Store values from the student points, and add them with the values that are not recorded, creating our variable
    # of the possible grade they can earn if all assignments are perfect.
    print('If student received a perfect grade throughout, he would have to earn a total of '
          '%d points, which would give him a %d%% at the end of the semester.' %
          ((full_points - full_student_pts), (possible_grade + full_points) / (len(asgnts_list))))
    # Calculate points to earn for the possible grade by subtracting student points by the max amount of points.
    # Calculate the percentage by taking the product of the possible grade added to the max amount of points and
    # dividing the product by the length of the assignments possible.
determine_grade() # Call our function.

# Reading file back inside program.
f = open('finalproject.py', 'r+')
contents = f.read()

print(contents)
f.close()
It's not wanting to display the output, but it puts the code inside. Can someone help me with this?
Reply
#2
Can you post your output. and also what output you are expecting? I kinda don't understand your question.
Don't doubt always question. There's a difference.
Reply
#3
This is an easy answer if you do simple debugging. Print asgnts_list, as you create it from a dictionary (and why not just use the dictionary). Dictionaries are in "hash" order, so you are printing/using a list that is in jumbled order as a simple print statement will tell https://www.tutorialspoint.com/python3/p...ionary.htm
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020