• <bdo id='MS780'></bdo><ul id='MS780'></ul>

    <tfoot id='MS780'></tfoot>
    <legend id='MS780'><style id='MS780'><dir id='MS780'><q id='MS780'></q></dir></style></legend>

    1. <small id='MS780'></small><noframes id='MS780'>

      <i id='MS780'><tr id='MS780'><dt id='MS780'><q id='MS780'><span id='MS780'><b id='MS780'><form id='MS780'><ins id='MS780'></ins><ul id='MS780'></ul><sub id='MS780'></sub></form><legend id='MS780'></legend><bdo id='MS780'><pre id='MS780'><center id='MS780'></center></pre></bdo></b><th id='MS780'></th></span></q></dt></tr></i><div id='MS780'><tfoot id='MS780'></tfoot><dl id='MS780'><fieldset id='MS780'></fieldset></dl></div>

      Int 转换不起作用

      时间:2023-09-11
      • <legend id='ySVX8'><style id='ySVX8'><dir id='ySVX8'><q id='ySVX8'></q></dir></style></legend>

          <i id='ySVX8'><tr id='ySVX8'><dt id='ySVX8'><q id='ySVX8'><span id='ySVX8'><b id='ySVX8'><form id='ySVX8'><ins id='ySVX8'></ins><ul id='ySVX8'></ul><sub id='ySVX8'></sub></form><legend id='ySVX8'></legend><bdo id='ySVX8'><pre id='ySVX8'><center id='ySVX8'></center></pre></bdo></b><th id='ySVX8'></th></span></q></dt></tr></i><div id='ySVX8'><tfoot id='ySVX8'></tfoot><dl id='ySVX8'><fieldset id='ySVX8'></fieldset></dl></div>
            <bdo id='ySVX8'></bdo><ul id='ySVX8'></ul>

                  <tbody id='ySVX8'></tbody>
                <tfoot id='ySVX8'></tfoot>

                <small id='ySVX8'></small><noframes id='ySVX8'>

                本文介绍了Int 转换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在为我的游戏创建一个高分功能,但我无法让它发挥作用

                I'm creating a highscore feature for my game but I cant get it to work

                这是我的方法:

                def game_over(self):
                    # Game over Screen
                    keys = pygame.key.get_pressed()
                    self.gameover = pygame.image.load('resources/screen/game_over.png')
                    screen.blit(self.gameover,(0,0))
                
                    high_filer = open('highscores.txt', 'r')
                    highscore = high_filer.read()
                    high_filer.close()
                    int(highscore)
                    int(self.score)
                    print highscore + self.score
                
                    if self.score > highscore: 
                        high_filew = open('highscores.txt', 'w')
                        high_filew.write(str(self.score))
                        high_filew.close()
                
                    if (keys[K_RETURN]):
                        self.state = 1
                

                它的作用是从 .txt 文件中读取最近的高分,并检查玩家的得分是否更高,如果是,它将新的高分写入文件

                What it does is reads the most recent highscore from a .txt file and checks if the players score is higher if it is it writes the new highscore into the file

                我使用 int(highscore) 将字符串从 highscore 转换为 int,然后在第 10 行执行 print highscore + self.score 作为测试,但我抛出一个错误,说我无法添加 str 和 int 即使我将 highscore 转换为 int 并且我转换了 self.score 所以出于某种原因之一转换无效

                I convert the string from highscore into a int by using int(highscore) then and on line 10 I do print highscore + self.score as a test but I throws an error that says that I can't add a str and an int even though I converted highscore to an int and I converted self.score so for some reason one of the conversions didn't work

                推荐答案

                int() 返回一个整数,但您丢弃了该结果.重新分配它:

                int() returns an integer, but you discard that result. Reassign it:

                highscore = int(highscore)
                

                该函数不会就地更改变量.如果 self.score 也是一个字符串,您需要对 int(self.score) 执行相同的操作,或者删除该行.

                The function does not change the variable in-place. If self.score is a string as well, you'll need to do the same thing for int(self.score), or just remove that line.

                这篇关于Int 转换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用整数作为输入的错误处理 下一篇:如何将字典中的字符串值转换为 int/float 数据类型

                相关文章

                最新文章

              1. <small id='NKcRN'></small><noframes id='NKcRN'>

                  <bdo id='NKcRN'></bdo><ul id='NKcRN'></ul>

                  <legend id='NKcRN'><style id='NKcRN'><dir id='NKcRN'><q id='NKcRN'></q></dir></style></legend>
                  <i id='NKcRN'><tr id='NKcRN'><dt id='NKcRN'><q id='NKcRN'><span id='NKcRN'><b id='NKcRN'><form id='NKcRN'><ins id='NKcRN'></ins><ul id='NKcRN'></ul><sub id='NKcRN'></sub></form><legend id='NKcRN'></legend><bdo id='NKcRN'><pre id='NKcRN'><center id='NKcRN'></center></pre></bdo></b><th id='NKcRN'></th></span></q></dt></tr></i><div id='NKcRN'><tfoot id='NKcRN'></tfoot><dl id='NKcRN'><fieldset id='NKcRN'></fieldset></dl></div>
                  1. <tfoot id='NKcRN'></tfoot>