英文:
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.
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("LINES",(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,16*BLOCK_SIZE+1,TEXT_COLOR)
pgb.text("%8s" % lines,(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,17*BLOCK_SIZE+1,TEXT_COLOR)
if score >= 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("HIGHSCORE",(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,16*BLOCK_SIZE+1,TEXT_COLOR)
pgb.text("%8s" % 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("LEVEL",(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,13*BLOCK_SIZE+1,TEXT_COLOR)
pgb.text("%8s" % 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("SCORE",(GRID_OFFSET+GRID_COLS+2)*BLOCK_SIZE+1,10*BLOCK_SIZE+1,TEXT_COLOR)
pgb.text("%8s" % 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("GAME OVER",WHITE)
pgb.show()
while True:
time.sleep(0.500)
When I run it, it outputs
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'
答案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) > 3:
highscore.pop()
update_highscore(100)
print(highscore)
update_highscore(300)
print(highscore)
update_highscore(250)
print(highscore)
update_highscore(150)
print(highscore)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论