如何循环此代码,学生每得50分,加1星

  • 本文关键字:50分 循环 代码 何循环 python
  • 更新时间 :
  • 英文 :


提前道歉,但我是python的初学者,仍在学习。我遇到了这样一个问题,学生在游戏中每获得50分,他们的信息就会增加1颗星。但我似乎做不到,这是我的代码;

class points:
def __init__ (self, studentname, points, star):
self.studentname = studentname
self.points = points
self.star = star
def play (self, totalpoints):
self.points += totalpoints
#every time the students score 50, they will have plus 1 star
if self.points == 50:
self.star += 1
def displayInfo (self):
print (self.studentname)
print(self.points)
print(self.star)
student1 = points("Ana", 0, 0)
student2 = points("Sandra", 0, 0)
student1.displayInfo()
student2.displayInfo() #will display their information before playing
student1.play(10)
student2.play(5)
student1.play(20)
student2.play(10)
student1.play(30)
student2.play(30)
student1.play(10)
student2.play(5)
student1.play(20)
student2.play(10)
student1.play(30)
student2.play(30)
student1.displayInfo()
student2.displayInfo() #will display their information after playing

当学生正好达到时,您只添加一颗星50分,再也不会了。你可以在play()方法中做的是在每次添加分数时更新玩家拥有的星星数量,如下所示:

def play(self, totalpoints):
self.points += totalpoints
self.star = self.points // 50

使用Python的floordiv操作符(//)

最新更新