mercredi 15 juin 2022

Why my code doesn't run when I use random.randint?

I am trying to make a wordle with python but finally when I run it, it doesn't work. First it seems to work but there is nothing in the output, then when I press CTRL+C to cancel the console gives me an error message: "line 26, in num = random.randint(0,(len(wordlist)-1)) keyboard interrupt"

CODE:

import random


listaPalabras = ["casa", "gato", "hola", "mesa"]


intentos = 5

class Colors:
    OKGREEN = '\033[92m'
    OKBLUE = '\033[94m'
    ENDC = '\033[0m'


def verde(pos):
    return Colors.OKGREEN+pos+Colors.ENDC

def azul(pos):
    return Colors.OKBLUE+pos+Colors.ENDC


resp = "S"

while resp.upper() == "S":

    # The problem appears in the next line
    num = random.randint(0,(len(listaPalabras)-1))

    sol = (listaPalabras[num])


for i in range (intentos):
    listaPalabra = []
    listasol = []

    palabra = input("\nEscribe una palabra de 4 letras:\n")

    while not (len(palabra) == 5):
        palabra = input("\nEscribe una palabra de 4 letras:\n")

    
    if sol == palabra:
        break

    else:
        for i in palabra:
            listaPalabra.append(i)

        for i in sol:
            listasol.append(i)

        for i in range (len(listaPalabra)):

            if listaPalabra[i] in listasol[i]:
                print(verde(listaPalabra[i], end = ""))

            if not listaPalabra[i] in listasol[i]:
                print(listaPalabra[i], end = "")

            elif listaPalabra[i] in sol and not (listaPalabra[i] in listasol[i]):
                print(azul(listaPalabra[i], end = ""))

print ("")

if palabra == sol:
    print(verde(palabra))
    print("La palabra es correcta!")

else:
    print("Se te han acabado los intentos\nNo has acertado")


resp = input ("Quieres volver a jugar? (S/N)")

while not resp.upper() in ["S", "N"]:
    resp = input ("Quieres volver a jugar? (S/N)")



Aucun commentaire:

Enregistrer un commentaire