我要求用户在一行中输入他们的分数和积分,用空格分隔,我已经把它变成了一个循环,这样当用户输入-1时,程序就会输出&#39 ;程序结束。'
input = map(float, raw_input().split())
gpa, credit = input
print 'Current GPA: %.2f' % gpa
while True:
if input[0] != -1:
input = map(float, raw_input().split())
gpa = ((gpa * credit) + (input[0] * input[1])) / (credit + input[1])
print 'Current GPA: %.2f' % gpa
if input [0] == -1:
print 'Program ends.'
break
sample output:
3.7 3
Current GPA: 3.70
3.3 2
Current GPA: 3.54
-1
Program ends.
但是我有这样的错误:
Output:
3.7 3
Current GPA: 3.70
3.3 2
Current GPA: 3.50
-1
Traceback (most recent call last):
File "/Users/tiffany/PycharmProjects/Labs/p3.py", line 8, in <module>
gpa = ((gpa * credit) + (input[0] * input[1])) / (credit + input[1])
IndexError: list index out of range
Process finished with exit code 1
有人可以告诉我如何解决这个问题吗?谢谢!