'Python always print one value when i run anyways

it always prints "2", please help me this is my codes:

ketqua1 = randint(1,6)
ketqua2 = randint(1,6)
ketqua3 = randint(1,6)

computer = ketqua1 + ketqua2 + ketqua3
if 10 < computer <= 18:
    computer = 1
elif computer <= 10:
    computer = 2


Solution 1:[1]

this is all my code:

 import pygame
from random import randint
def draw_floor():
    screen.blit(floor, (floorrunning, 453))
    screen.blit(floor, (floorrunning + 800, 453))
                
pygame.init()
bg = pygame.image.load("background.png")

screen = pygame.display.set_mode((800,500))

GREY = (120, 120, 120)
WHITE = (255, 255, 255)

floor = pygame.image.load("quangcaobanthan.png")
floorrunning = 0

xucxac1 = pygame.image.load("nan1.png")
xucxac2 = pygame.image.load("nan1_2.png")
xucxac3 = pygame.image.load("nan1_3.png")
xucxac1_rect = (141, 300)
xucxac2_rect = (341, 300)
xucxac3_rect = (541, 300)

youlose = pygame.image.load("youlose.png")
youlose_rect = youlose.get_rect(center = (400, 250))
youwin = pygame.image.load("youwin.png")
youwin_rect = youwin.get_rect(center = (400, 250))

running = True
replay = True

ketqua1 = randint(1,6)
ketqua2 = randint(1,6)
ketqua3 = randint(1,6)

computer = ketqua1 + ketqua2 + ketqua3
if 10 < computer <= 18:
    computer = 1
elif computer <= 10:
    computer = 2

game_font = pygame.font.SysFont("04B_19.ttf", 40)
player = 0
def ketqua():
    ketqua1_infor = game_font.render(str(ketqua1), True, (0,0,0))
    ketqua1_rect = (190, 334)
    screen.blit(ketqua1_infor, ketqua1_rect)
    ketqua2_infor = game_font.render(str(ketqua2), True, (0,0,0))
    ketqua2_rect = (390, 334)
    screen.blit(ketqua2_infor, ketqua2_rect)
    ketqua3_infor = game_font.render(str(ketqua3), True, (0,0,0))
    ketqua3_rect = (590, 334)
    screen.blit(ketqua3_infor, ketqua3_rect)

while running:

    screen.blit(bg,(0,0))
    mouse_x, mouse_y = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and replay == False:
                replay = True
                xucxac1_rect = (141, 300)
                xucxac2_rect = (341, 300)
                xucxac3_rect = (541, 300)
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if (100 < mouse_x < 300) and (100 < mouse_y < 221):
                    player = 1
                if (500 < mouse_x < 700) and (100 < mouse_y < 211):
                    player = 2      
    if xucxac1_rect == (41, 300) and xucxac2_rect == (241, 300) and xucxac3_rect == (441, 300):
            screen.blit(youwin, youwin_rect)
            screen.blit(youlose, youlose_rect)
    pygame.draw.rect(screen, WHITE, (100, 100, 200, 121))
    pygame.draw.rect(screen, WHITE, (500, 100, 200, 111))
    pygame.draw.rect(screen, WHITE, (277, 0, 240, 100))

    screen.blit(pygame.image.load("tai.png"), (100,100))
    screen.blit(pygame.image.load("xiu.png"), (500,100))
    screen.blit(pygame.image.load("start.png"), (275, -60))

    print(computer)
    draw_floor()
    floorrunning -= 0.5
    if floorrunning <= -800:
        floorrunning = 0
    if replay == False:
        ketqua1 = randint(1,6)
        ketqua2 = randint(1,6)
        ketqua3 = randint(1,6)

    if replay:
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if (141 < mouse_x < 241) and (300 < mouse_y < 400):
                    xucxac1_rect = (41, 300)
                if (341 < mouse_x < 441) and (300 < mouse_y < 400):
                    xucxac2_rect = (241, 300)
                if (541 < mouse_x < 641) and (300 < mouse_y < 400):
                    xucxac3_rect = (441, 300)
                if (277 < mouse_x < 527) and (0 < mouse_y < 79):
                    replay = False
        pygame.draw.rect(screen, GREY, (50, 290, 700, 120))
        pygame.draw.circle(screen, GREY, (195, 350), 48)
        pygame.draw.circle(screen, GREY, (395, 350), 48)
        pygame.draw.circle(screen , GREY,(595, 350), 48)

        screen.blit(xucxac1, xucxac1_rect)
        screen.blit(xucxac2, xucxac2_rect)
        screen.blit(xucxac3, xucxac3_rect)

    ketqua()
    pygame.display.flip()

pygame.quit()

Solution 2:[2]

Your code runs randomly. That means there is a change that you get 1 day, one week, one month the same answer.

Assuming that you have from random import randint Then you could thy this:

def execute():
  ketqua1 = randint(1,6)
  ketqua2 = randint(1,6)
  ketqua3 = randint(1,6)
  
  computer = ketqua1 + ketqua2 + ketqua3
  if 10 < computer <= 18:
      computer = 1
  elif computer <= 10:
      computer = 2
  return computer

histogram = [0,0,0]
for _ in range(100):
  c = execute()
  histogram[c] += 1

print("Times we got computer==1: {}".format(histogram[1]))
print("Times we got computer==2: {}".format(histogram[2]))

You should have something like this:

Times we got computer==1: 49
Times we got computer==2: 51

So randint is being used well...

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 Nguy?n Vi?t Minh
Solution 2 Fabian_57