简体   繁体   English

是Python的新手。.GradepointAVG?

[英]New to Python.. Gradepoint AVG?

Rather than asking the user to enter a number, modify gpa.py to enter 5 grades, but make it letter grades (A, A-, B+, etc). 而不是要求用户输入数字,而是修改gpa.py以输入5个等级,但使其成为字母等级(A,A-,B +等)。 Please input these grades, convert them to 4 point scale equivalents, (a=4.0, a- = 3.7, B+ =3.3, B= 3.0, B- = 2.7, C+ =2.3, C = 2.0, C- =1.7 etc.). 请输入这些等级,并将其转换为4个点刻度等值,(a = 4.0,a- = 3.7,B + = 3.3,B = 3.0,B- = 2.7,C + = 2.3,C = 2.0,C- = 1.7等。 )。 Print out the numeric equivalent of the average of these 5 grades. 打印出这五个等级的平均值的数值等效项。

originally for the first program i did i had 本来我做的第一个程序

print("This program calculates grade point average.")
g1 = float(input("Enter course 1 grade -> "))
g2 = float(input("Enter course 2 grade -> "))
g3 = float(input("Enter course 3 grade -> "))
g4 = float(input("Enter course 4 grade -> "))
g5 = float(input("Enter course 5 grade -> "))
sum = g1 + g2 + g3 + g4 + g5
count = 5
print ("Your GPA for last semester was -> ", sum / count)

but after reading this it tells me it wants me to assign grades, so i made it this 但是看完这篇文章告诉我它要我分配成绩,所以我做到了

print("This program calculates grade point average.")
g1 = a
g2 = b
g3 = c
g4 = c+
g5 = b-
sum = g1 + g2 + g3 + g4 + g5
count = 5
print ("Your GPA for last semester was -> ", sum / count)

how can i make the grades from strings to ints and then give the grade point average? 我怎样才能使分数从字符串到整数,然后给平均分数?

You can use a dict to lookup numeric values based on the input string 您可以使用dict根据输入字符串查找数字值

>>> grades = {'A':4.0, 'A-':3.7, 'B+':3.3, 'B':3.0 ... etc}
>>> grades['B']
3.0

Your method is a long of way of accomplishing the task. 您的方法是完成任务的很长的路要走。 Use dictionaries like the user above has demonstrated. 像上面的用户一样使用字典已演示。 They can grow and decrease in size and will be much easier storing large amounts of data. 它们可以增大和减小大小,并且将更容易存储大量数据。

You can use a dict as suggested to map note letters to their numerical equivalent. 您可以按照建议使用dict来将备忘字母映射到其数值等效项。 Do not forget to surround it with a try in case of a wrong grade is given (ie Z). 如果给出了错误的等级(即Z),也不要忘记try
Also, it is way more readable to use lists and loops instead of repeating the same code. 同样,使用列表和循环而不是重复相同的代码也更具可读性。

# Grades dictionnary
letter_grades = {
    'A' : 4.0,
    'A-' : 3.7, 
    'B+' : 3.3, 
    'B' : 3.0, 
    'B-' : 2.7, 
    'C+' : 2.3, 
    'C' : 2.0, 
    'C-' : 1.7,
    # and so on...
}

# Total grades
grades_number = 3
# List of grades
grades = []

print("This program calculates grade point average.")
for i in range(grades_number):
    grade = input("Enter course {} grade -> ".format(i+1))
    grades.append(grade)

try:
    # We try to sum the grades, but if a given one is wrong, then a KeyError with be thrown
    sum = sum([letter_grades[i] for i in grades])
    print ("Your GPA for last semester was -> ", sum / len(grades))
except KeyError:
    print("You entered a wrong grade.")

I have gotten it to work! 我已经开始工作了!

grades = {
'A':4.0,
'A-':3.7,
'B+':3.3,
'B':3.0,
'B-':2.7,
'C+':2.3,
'C':2.0,
'C-':1.7,
'D+':1.3,
'D':1.0,
'D-':0.7,
'F':0.0
}

print("This program calculates grade point average.")


g1 = grades['C']
g2 = grades['A']
g3 = grades['F']
g4 = grades['B-']
g5 = grades['D+']
total = g1 + g2 + g3 + g4 + g5
totalgrades = 5
print ("Your GPA for last semester was -> ", total / totalgrades)

Adding a dictionary helped the most. 添加字典最有帮助。 But I came to a point where i was getting an error at the 20th line(g1 = grades['A']) I had not put the grades part before ['A'] and thats what was wrong with it. 但是我到了第20行出现错误的地方(g1 =成绩['A']),我没有将成绩部分放在['A']之前,这就是问题所在。 all works now and good to go. 现在一切正常,一切顺利。 Thank you everyone for the help. 谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM