我试图为我的最终项目使用GUI编写GPA计算器。我对其中的一些进行了编码,然后卡在其中。
import math
x = int(input('How many courses are you taking this year?'))
marklst = []
i = 1
while i <= x:
y = int(input('What is your mark for this class?'))
marklst.append(y)
i += 1
percentage_calculation = sum(marklst)/(x)
print(percentage_calculation)
for percentage_calculation in range(x,y):
print('Your GPA is 1')
for percentage_calculation in range(w,z):
print('Your GPA is 2')
for percentage_calculation in range(o,p):
print('Your GPA is 3')
有没有一种更快的打印GPA的方法,而无需编写10行以进行(x,y)范围内的percent_calculation
答案 0 :(得分:0)
我认为,只要将for
更改为if
,就可以正常工作。
如果您正在寻找一种较短的方法来代替此部分:
x = int(input('How many courses are you taking this year?'))
marklst = []
i = 1
while i <= x:
y = int(input('What is your mark for this class?'))
marklst.append(y)
i += 1
您可以执行以下操作:
marklst = input("Please enter your marks for this year seperated by commas:")
# Let's suppose the user entered 5,4,3,5,4 which will give you a string
# Then you can split and cast them to ints
marklst = [int(mark) for mark in marklst.split(",")]
# Your percentage calculation will be
percentage_calculation = sum(marklst)/(len(marklst))
我想您仍然必须具有if语句才能进行比较和打印。