Home > Mobile >  How do i reference a dictionary using a list as the key?
How do i reference a dictionary using a list as the key?

Time:11-09

So I want to reference a dictionary using a list as the key.

import random
gradePoints = {"A":4,"B":3,"C":2,"D":1,"F":0}

courseList = ["CST 161","Mat 144","ENG 201","PSY 101","HIS 101"]

gradeList = ["A","B","C","D","F"]

creditList = [3,4]

totalCredits=0

qualityPoints=0

print("Course..Grade..Value Per Course")
for i in range(0,len(courseList)):
    print(courseList[i]," ",end='')

    
    gPoints=random.randint(0,len(gradePoints))  
    creditHours=random.randint(0,len(creditList))
    
    valuePerCourse= (gPoints*creditHours)
    
    totalCredits=creditHours
    
    qualityPoints=valuePerCourse
    
    print(gPoints)
    

gpa=qualityPoints/totalCredits
print("GPA is: ", round(gpa,2))

I attempted to use a nested loop to include the letters and all that happened was the result being all over the place, rather than being printed next to the courseList it was printed in the next line over making it look nasty. My expectation was to have gradeList print next to courseList and next to that would be the gradePoint depending on what is pulled from gradeList

Course..Grade..Value Per Course
CST 161  2
Mat 144  2
ENG 201  2
PSY 101  2
HIS 101  2
GPA is:  2.0

this is what i get, its meant to be randomized

Course..Grade..Value per course
CST 161 A   4
ENG 101 B   3
MAT 119 B   4

something like this is what i want

im meant to write a program that takes this dictionary gradePoints = {"A":4,"B":3,"C":2,"D":1,"F":0}

and this list:

courseList = ["CST 161","Mat 144","ENG 201","PSY 101","HIS 101"]

and randomly use elements from each of these lists :

gradeList = ["A","B","C","D","F"]

creditList = [3,4]

and produce a grade point average.

the gradeList element will be embedded as the key in referencing the grade points.

(e.g. gPoints = gradePoints[random gradeList element])

(e.g. creditHours = random creditList element).

To get a random list element, use the randint method with a range of (0,len(list)).

That will be multiplied by the gPoints per course.

Multiply them to get the the valuePerCourse for the that course.

(e.g. valuePerCourse = (creditHours * gPoints), this will yield the value for that course.

For each course (each pass through the loop),

add creditHours to totalCredits and

add valuePerCourse to qualityPoints

qualityPoints divided by total Credits results in a GPA.

Display each course with their grade, and the value per course.

Display the quality points and the GPA.

Display the proper string literals that will identify your output.

CodePudding user response:

Try:

import random
gradePoints = {"A":4,"B":3,"C":2,"D":1,"F":0}
courseList = ["CST 161","Mat 144","ENG 201","PSY 101","HIS 101"]
gradeList = ["A","B","C","D","F"]
creditList = [3,4]
totalCredits=0
qualityPoints=0
print("Course..Grade..Value Per Course")
gpa = 0
for i in range(0,len(courseList)):
    print(courseList[i]," ",end='')
    gPoints=random.randint(0,len(gradePoints)-1) # <<<  
    creditHours=creditList[random.randint(0,len(creditList)-1)] # <<<
    valuePerCourse= (gPoints*creditHours)
    totalCredits=creditHours
    qualityPoints=valuePerCourse
    print(list(reversed(gradeList))[gPoints], '   ', gPoints) # <<<
    gpa  = qualityPoints/totalCredits   # <<<

print("   GPA is: ", round(gpa, 2)) # <<<

Changed lines are marked with # <<<

Gives:

Course..Grade..Value Per Course
CST 161  F     0
Mat 144  D     1
ENG 201  A     4
PSY 101  F     0
HIS 101  F     0
   GPA is:  5.0
  • Related