Why does my highscore saving output "TypeError: unsupported types for __ge__: 'int', 'list'" in micropython?

huangapple go评论80阅读模式
英文:

Why does my highscore saving output "TypeError: unsupported types for __ge__: 'int', 'list'" in micropython?

问题

当我运行它时,它会输出以下错误信息:

Traceback (most recent call last):
  File "<stdin>", line 608, in <module>
  File "<stdin>", line 409, in game_over_screen
TypeError: unsupported types for __ge__: 'int', 'list'

这个错误信息是因为在代码中的这一行:

if score >= highscore:

"score" 是一个整数,而 "highscore" 是一个列表(list),所以无法直接进行大于等于('ge')的比较。您需要确保它们都是可以比较的类型,或者更正您的比较逻辑以适应您的需求。

请注意,这只是错误信息的翻译,不包括解决方法。如果您需要解决问题,您可能需要检查您的代码以找到错误并进行修复。

英文:

Why does my highscore saving output "TypeError: unsupported types for ge: 'int', 'list'" in micropython?

I tried making my Tetris game on my RaspberryPi Pico be able to save highscores, but I kept getting errors. I dont understand whats wrong with my code. I've tried looking up how to save highscores, none of them worked. Why does my highscore saving output "TypeError: unsupported types for __ge__: 'int', 'list'" in micropython?

        def game_over_screen():
            pgb.fill_rect(240-240,240-240,240,240,PicoGameBoy.color(0,0,0))
            pgb.fill_rect((GRID_OFFSET+GRID_COLS+1)*BLOCK_SIZE+1,16*BLOCK_SIZE,
                          BLOCK_SIZE*7,BLOCK_SIZE*2,
                          TEXT_BACKGROUND_COLOR)
            pgb.text(&quot;LINES&quot;,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,16*BLOCK_SIZE+1,TEXT_COLOR)
            pgb.text(&quot;%8s&quot; % lines,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,17*BLOCK_SIZE+1,TEXT_COLOR)
            if score &gt;= highscore:
                highscore.pop(0)
                highscore.append(score)
            pgb.fill_rect((GRID_OFFSET+GRID_COLS+1)*BLOCK_SIZE+1,16*BLOCK_SIZE,
                          BLOCK_SIZE*7,BLOCK_SIZE*2,
                          TEXT_BACKGROUND_COLOR)
            pgb.text(&quot;HIGHSCORE&quot;,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,16*BLOCK_SIZE+1,TEXT_COLOR)
            pgb.text(&quot;%8s&quot; % highscore,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,17*BLOCK_SIZE+1,TEXT_COLOR)
            # draw text (LEVEL)
            pgb.fill_rect((GRID_OFFSET+GRID_COLS+1)*BLOCK_SIZE+1,13*BLOCK_SIZE,
                          BLOCK_SIZE*7,BLOCK_SIZE*2,
                          TEXT_BACKGROUND_COLOR)
            pgb.text(&quot;LEVEL&quot;,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,13*BLOCK_SIZE+1,TEXT_COLOR)
            pgb.text(&quot;%8s&quot; % level,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,14*BLOCK_SIZE+1,TEXT_COLOR)
            
            # draw text (SCORE)
            pgb.fill_rect((GRID_OFFSET+GRID_COLS+1)*BLOCK_SIZE+1,10*BLOCK_SIZE,
                          BLOCK_SIZE*7,BLOCK_SIZE*2,
                          TEXT_BACKGROUND_COLOR)
            pgb.text(&quot;SCORE&quot;,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,10*BLOCK_SIZE+1,TEXT_COLOR)
            pgb.text(&quot;%8s&quot; % score,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,11*BLOCK_SIZE+1,TEXT_COLOR)
            #pgb.fill_rect(60,90,120,240,BLACK)
            pgb.center_text(&quot;GAME OVER&quot;,WHITE)
            pgb.show()
            while True:
                time.sleep(0.500)

When I run it, it outputs

Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 608, in &lt;module&gt;
  File &quot;&lt;stdin&gt;&quot;, line 409, in game_over_screen
TypeError: unsupported types for __ge__: &#39;int&#39;, &#39;list&#39;

答案1

得分: 1

分数是一个整数值,高分是一个整数值的列表,所以你不能直接比较它们。你需要遍历高分列表,将每个高分与分数进行比较。或者你可以直接将新的分数添加到列表中,然后对高分进行排序,保留前三名。

highscore = []

def update_highscore(new_score):
    highscore.append(new_score)   # 添加新分数
    highscore.sort(reverse=True)  # 将高分按降序排序

    # 保留前三名分数
    if len(highscore) > 3:
        highscore.pop()

update_highscore(100)
print(highscore)

update_highscore(300)
print(highscore)

update_highscore(250)
print(highscore)

update_highscore(150)
print(highscore)
英文:

Score is an integer value and highscore is a list of integer values, so you cant directly compare the two. You need to walk through the list of highscores and compare each one with the score. Or you could just append the new score, sort the highscores and keep the top 3.

highscore = []

def update_highscore(new_score):
    highscore.append(new_score)   # append the new score
    highscore.sort(reverse=True)  # Sort the scores in descending order

    # Keep only the top three scores
    if len(highscore) &gt; 3:
        highscore.pop()

update_highscore(100)
print(highscore)

update_highscore(300)
print(highscore)

update_highscore(250)
print(highscore)

update_highscore(150)
print(highscore)

huangapple
  • 本文由 发表于 2023年5月28日 02:50:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定