'How can I fix this Random and Pygame library error?

I wanted to make a "Who Wants to be a millionaire?" type game, and I want to test it out via a thing where if I press down the left mouse button, and random number generator generates a number from a list and it draws the code on the screen that belongs to the number and removes that number.

Now, the problem is that I tried it literally every single way, but it keeps appearing more than twice. The code under now is trying to say that if we are out of the numbers, then change the screen to black. Now, this is not working, it gives an error like this:

none
2
Traceback (most recent call last):
  File "C:\Users\username\PycharmProjects\joemama\Farsang\Kérdések\main.py", line 24, in <module>
    result = random.choice(number)
  File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\random.py", line 346, in choice
    return seq[self._randbelow(len(seq))]
IndexError: list index out of range
1

Here is the code:

import pygame
import os
import random

WIDTH, HEIGHT = (1024, 768)
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Joe mama")
kerdes1 = pygame.image.load("Zene/1/Kérdés-1.png")
kerdes2 = pygame.image.load("Zene/2/Kérdés-2.png")

number = [1, 2]
numbers = 2

def number_one():
    WIN.blit(kerdes1, (0, 0))

def number_two():
    WIN.blit(kerdes2, (0, 0))

while True:
    result = random.choice(number)
    if len(number) == 0:
        break
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if result == 1:
                    print("1")
                    number.remove(1)
                    numbers -= 1
                    number_one()
                    if numbers == 0:
                        WIN.fill((0, 0, 0))
                        break
                elif result == 2:
                    print("2")
                    number.remove(2)
                    numbers -= 1
                    number_two()
                    if numbers == 0:
                        WIN.fill((0, 0, 0))
                        break
                pygame.display.update()

What can I try next?



Solution 1:[1]

I'm not exactly sure which behavior you want to have in the end, but how about this one?

First, you change this part of code right after the while True: from

result = random.choice(number)
if len(number) == 0:
    break

to

if len(number) > 0:
    result = random.choice(number)

In this way, you will only choose a number if there is still one left in the list.


After that you also change the code below the if statement where you test for pygame.MOUSEBUTTONDOWN to

if event.type == pygame.MOUSEBUTTONDOWN:
    if event.button == 1:
        if len(number) == 0:
            WIN.fill((0, 0, 0))
        else:
            if result == 1:
                print("1")
                number.remove(1)
                numbers -= 1
                number_one()

            elif result == 2:
                print("2")
                number.remove(2)
                numbers -= 1
                number_two()

        pygame.display.update()

Now, when you press the mousebutton and there is no number left to choose from, you fill the screen with a solid black. I also deleted the break statements you had in there, because when you fill the screen black and break afterwards, the game will end immediately and you cannot see the change.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sven