-1

Im pretty new in python and I want to make a little game to guess the airplane. In the below code i find a problem when "valor_posicion_random" is 2, the problem in the Title appear and I dont know why.

import random
x=0
res=0
lis_av = { 1:"Airbus A380" , 2:"Airbus A340" , 3:"Airbus A330" , 4:"Boeing 777" , 5:"Boeing 787" , 6:"Boeing 747"}
def posicion_random(lis_av, res):
    print(lis_av[valor_avion])
    if valor_posicion_random == 1:
        print("\n 1.",lis_av[valor_avion],"\n 2.",random.choice(lis_av))
    elif valor_posicion_random == 2:
        print("\n 1." , random.choice(lis_av), "\n 2.",lis_av[valor_avion])
    res=int(input("What plane is it?"))
print(lis_av)
while x==0:
    valor_avion = random.randint(1,6)
    valor_posicion_random = random.randint(1,2)
    posicion_random(lis_av, res)
    x=int(input("Try again? (1=No//0=Yes)"))

Pd. I'll use PIL to put an image to guess the airplane but for the moment I use the print below the def function to know the airplane Pd2. I am latin, sorry for the latin variables <3

  • Please provide more details about the error you get – Filip Müller Jul 28 '22 at 17:28
  • Does this answer your question? [How to randomly select an item from a dictionary?](https://stackoverflow.com/questions/51450258/how-to-randomly-select-an-item-from-a-dictionary) – 001 Jul 28 '22 at 17:39

1 Answers1

1

In your code the sometime the error is come for random.choice(lis_av) And the error is seq[self._randbelow(len(seq))] . For solving this problem you need to modify your lis_av variables to

lis_av = {0:"your value", 1:"Airbus A380" , 2:"Airbus A340" , 3:"Airbus A330" , 4:"Boeing 777" , 5:"Boeing 787" , 6:"Boeing 747"}

or

use lis_av[random.choice(list(lis_av))] without using random.choice(lis_av)

Full code

import random
x=0
res=0
lis_av = { 1:"Airbus A380" , 2:"Airbus A340" , 3:"Airbus A330" , 4:"Boeing 777" , 5:"Boeing 787" , 6:"Boeing 747"}
def posicion_random(lis_av, res):
    print(lis_av[valor_avion])
    if valor_posicion_random == 1:
        print("\n 1.",lis_av[valor_avion],"\n 2.",lis_av[random.choice(list(lis_av))])
    elif valor_posicion_random == 2:
        print("\n 1." , lis_av[random.choice(list(lis_av))], "\n 2.",lis_av[valor_avion])
    res=int(input("What plane is it?"))
print(lis_av)
while x==0:
    valor_avion = random.randint(1,6)
    valor_posicion_random = random.randint(1,2)
    posicion_random(lis_av, res)
    x=int(input("Try again? (1=No//0=Yes)"))

checkout this blog

https://moonbooks.org/Articles/How-to-select-randomly-keys-from-a-dictionary-in-python-3-/