首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何循环这个代码,每一个学生得分50分,就会增加1颗星。

如何循环这个代码,每一个学生得分50分,就会增加1颗星。
EN

Stack Overflow用户
提问于 2021-09-06 05:33:11
回答 1查看 51关注 0票数 0

事先道歉,但我是蟒蛇的初学者,而且还在学习。我遇到了这个问题,学生在比赛中每赚50分,就会在他们的信息中添加1颗星。但我似乎做不到,这是我的代码;

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-06 05:39:04

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

代码语言:javascript
复制
def play(self, totalpoints):
    self.points += totalpoints
    self.star = self.points // 50

这使用Python's floordiv operator (//)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69069526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档