The condition of the while loop is checked at the start of each iteration. So what happens is that the level becomes 0 only after it gets checked, and doesn't exit right away.
The sequence of events:
level is set to 1
while loop entered
start of first iteration: exit if level is not 0
set level to (0 in this case)
do your try-except
You can see that when the while
condition is checked, level is not 0, and therefore the first iteration still executes.
A good way to get around this problem is shown at https://wiki.python.org/moin/WhileLoop.
In this case, you could do
level = 1
while True: #exiting of the loop is handled with the break
level = int(input("Enter level (1, 2, 3)\n")) % 4
if level==0: #the loop condition is inverted and moved here
break
try:
g = ply.level_high(level)
print("Player with highest level", level, "score is", g[0][0], "with", g[0][level])
except:
print("Invalid level")