我需要有关Python编码问题的帮助。
我必须计算学生的GPA。该程序必须询问他们正在上多少课,然后要求他们输入每个班级的成绩以及是否加权。
然后程序应输出平均GPA(包括小数位),而我的主程序必须调用该函数。
问题给出了与给定字母等级相关的非加权和加权数字分数的图表: gpa chart
这是我到目前为止的内容:
def average (c):
div = c
avg =(1.0*(sum(scores))/div)
#****************MAIN********************
c = input("How many classes are you taking?")
print ("You are taking " + str(c) + " classes.")
x = input("Enter your letter grade.")
w = int(input("Is it weighted? (1 = yes)")
while (c <= 7): #Here it is saying that there is a syntax error because I input a while loop. I don't know why it is saying this.
if (x == A):
if (w == 1):
print ("Your GPA score is 5")
else:
print ("Your GPA score is 4")
elif (x == B):
if (w == 1):
print ("Your GPA score is 4")
else:
print ("Your GPA score is 3")
elif (x == C):
if (w == 1):
print ("Your GPA score is 3")
else:
print ("Your GPA score is 2")
elif (x == D):
if (w == 1):
print ("Your GPA score is 2")
else:
print ("Your GPA score is 1")
elif (x == F):
if ( w == 1):
print ("Your GPA score is 1")
else:
print ("Your GPA score is 0")
scores = []
list.append(x)
average(c)
任何帮助将不胜感激! :)
答案 0 :(得分:0)
不确定您要的是什么,但是在函数定义之后进行修复可能是一个不错的开始
不要
sqrt("a")
要做
library(Jmisc)
library(dplyr)
x <- data.frame(k1 = c(3,"a",3,4,5), k2 = c(1,NA,NA,4,5), data1 = 1:5)
p <- data.frame(NULL)
for (row in 1:nrow(x)){
sqrtd <- tryCatch(sqrt(x$k1[row]),error=function(e) sqrtd = "NULL")
x <- addCol(x,value=c(sqr=sqrtd))
p <- rbind(p,x)
}
print(p)
答案 1 :(得分:0)
也许是这样?我没有在课程中划分任何内容,但我希望您了解其逻辑:
number_class=None
while number_class==None:# I set-it up number_class=None so it will keep asking the number of classes until you introduce an integer value
try:
number_class=input("How many classes are you taking?")
except:
print "Error, the input should be a number!\n"
total_result=0#your score start from 0
i=0
while i<number_class:# i create a loop to insert the score for each class
grade=raw_input("Enter your letter grade for the %s class." %(str(i+1)))
grade=grade.upper()#convert the grate to upper so you are able to introduce a or A without make too many check
if grade== 'A' or grade== 'B' or grade== 'C' or grade== 'D' or grade== 'F':#if you introduce a correct score we add it to the total sum and we go ahead with next class
i+=1#next iteration
if grade== 'A':
total_result+=4
elif grade== 'B':
total_result+=3
elif grade== 'C':
total_result+=2
elif grade== 'D':
total_result+=1
#elif: grade== 'F':#we can omitt F seeing that it's =0
# total_result+=0
print total_result
else:# if you introduce a wrong input for the grade we ask you again without pass to next iteration
print "Error, the grade should be: A, B, C, D or F!\n"
average="%.2f" % (float(total_result)/float(number_class))#we divided the total score for the number of classes using float to get decimal and converting on 2 decimal places
print "Your GPA is: %s" %(str(average))
示例:
How many classes are you taking?5
Enter your letter grade for the 1 class.A
4
Enter your letter grade for the 2 class.B
7
Enter your letter grade for the 3 class.C
9
Enter your letter grade for the 4 class.A
13
Enter your letter grade for the 5 class.B
16
Your GPA is: 3.20