class User:
def __init__(self, name, grades):
self.name = name
self.grades = grades
def average(self):
return sum(self.grades) / len(self.grades)
userName = input("Enter your name: ")
userGrades = []
while True:
userGradesInput = input("Enter your grades and write 'stop' to stop entering: ")
if userGradesInput == int:
userGrades.append(userGradesInput)
if userGradesInput == "stop":
break
print(userGrades)
因此,当您运行该程序并不断增加年龄时,该列表仍然为空。有人可以告诉我问题出在哪里,请不要使其复杂化。
我猜您正在尝试将输入(等级)附加到列表中,只是它是整数。您应该这样做:
userGradesInput = input("Enter your grades and write 'stop' to stop entering: ")
if userGradesInput.isdigit(): # check whether input is int or not like this
userGrades.append(int(userGradesInput))
elif userGradesInput == "stop":
break
当您if userGradesInput == int:
不实际检查它是否为整数时,您将比较它是否等于<class 'int'>
Python中的内置类型,这是错误的且毫无意义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句