How to fix ValueError: invalid literal for int() with base 10: ‘import’ error Pygame Zero

I have an error in Pygame Zero and I have questions about it. Full code(error statement):

pygame 2.0.0 (SDL 2.0.12, python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:UsersSamuel JeungSaved GamesPycharmProjectsCoding With Pythonballoon-flightballoon.py", line 123, in <module>
    pgzrun.go()
  File "C:UsersSamuel JeungAppDataRoamingPythonPython39site-packagespgzrun.py", line 31, in go
    run_mod(mod)
  File "C:UsersSamuel JeungAppDataRoamingPythonPython39site-packagespgzerorunner.py", line 113, in run_mod
    PGZeroGame(mod).run()
  File "C:UsersSamuel JeungAppDataRoamingPythonPython39site-packagespgzerogame.py", line 217, in run
    self.mainloop()
  File "C:UsersSamuel JeungAppDataRoamingPythonPython39site-packagespgzerogame.py", line 252, in mainloop
    update(dt)
  File "C:UsersSamuel JeungAppDataRoamingPythonPython39site-packagespgzerogame.py", line 194, in <lambda>
    return lambda dt: update()
  File "C:UsersSamuel JeungSaved GamesPycharmProjectsCoding With Pythonballoon-flightballoon.py", line 120, in update
    update_high_scores()
  File "C:UsersSamuel JeungSaved GamesPycharmProjectsCoding With Pythonballoon-flightballoon.py", line 36, in update_high_scores
    if(score > int(high_score)):
ValueError: invalid literal for int() with base 10: 'import'
[Finished in 45.3s]

So, yeah. I should give you(whoever is reading this) the full* code.

import pgzrun

from random import randint

bird_up = True
up = False
game_over = False
score = 0
number_of_updates = 0

scores = []

def update_high_scores():
    global score, scores
    filename = r"C:/Users/Samuel Jeung/Saved Games/PycharmProjects/Coding With Python/balloon-flight/balloon.py"
    scores = []
    with open(filename, "r") as file:
        line = file.readline()
        high_scores = line.split()
        for high_score in high_scores:
            if(score > int(high_score)):
                scores.append(str(score) + " ")
                score = int(high_scores)
            else:
                scores.append(str(high_score) + " ")
    with open(filename, "w") as file:
        for high_scores in scores:
            file.write(high_score)

def display_high_scores():
    screen.draw.text("HIGH SCORES", (350, 150), color="black")
    y = 170
    position = 1
    for high_score in scores:
        screen.draw.text(str(position) + ". " + high_score, (350, y), color="black")
        y += 25
        position += 1

def draw():
    screen.blit("background", (0, 0))
    if not game_over:
        balloon.draw()
        bird.draw()
        house.draw()
        tree.draw()
        screen.draw.text("Score: " + str(score), (700, 5), color=("black"))
    else:
        display_high_scores()

Not that important

pgzrun.go()

This is the high_score txt(which isn’t upgraded because of the error):

0, 0, 0

Hope I get an answer soon.

-Samuel Jae-Hoon Jeung

Answer

Look at this statement:

 File "C:UsersSamuel JeungSaved GamesPycharmProjectsCoding With Pythonballoon-flightballoon.py", line 36, in update_high_scores
if(score > int(high_score)): ValueError: invalid literal for int() with base 10: 'import'

Actually I’m trying to guess what you really wanna do by this code. Maybe your filename shouldn’t be your code, but your ‘high_score txt’?

‘import’ is the first word of your code right? Your code are scaning itself.

Leave a Reply

Your email address will not be published. Required fields are marked *