dimanche 23 juillet 2023

strange behavior with randint in python

rol_dict = { 0: "value 1", 1: "value 2", 2: "value 3" }

def rol_rand():
    global rol_list
    return rol_dict.get(randint(0, 2))

rol_rand()

if rol_rand() == rol_dict.get(0):
    print("value 1")
elif rol_rand() == rol_dict.get(1):
    print("value 2")
elif rol_rand() == rol_dict.get(2):
    print("value 3")
else:
    print(rol_rand())
    print("rol don't exist")

For this code some times appear this output

value 1
rol don't exist

why???

Why does this behavior occur and what alternatives exist?




Aucun commentaire:

Enregistrer un commentaire