[Python] Why is my code checking the whole dictionary even if the condition was checked?

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024.

  1. Stack

    Stack Membro Participativo

    The problem is in the line 320 and onward

    import tkinter as tk
    from tkinter import *
    import random
    import pypokedex

    root = tk.Tk() #main

    jugadores = {} #players
    evoluciones = {1: 3, 2: 3,
    4: 6, 5: 6,
    7: 9, 8: 9,
    10: 12, 11: 12,
    13: 15, 14: 15,
    16: 18, 17: 18,
    19: 20,
    21: 22,
    23: 24,
    172: 26, 25: 26,
    27: 28,
    29: 31, 30: 31,
    32: 34, 33: 34,
    173: 36, 35: 36,
    37: 38,
    174: 40, 39: 40,
    41: 169, 42: 169,
    43: 44,
    46: 47,
    48: 49,
    50: 51,
    52: 53,
    54: 55,
    56: 57,
    58: 59,
    60: 61,
    63: 65, 64: 65,
    66: 68, 67: 68,
    69: 71, 70: 71,
    72: 73,
    74: 76, 75: 76,
    77: 78,
    81: 462, 82: 462,
    84: 85,
    86: 87,
    88: 89,
    90: 91,
    92: 94, 93: 94,
    95: 208,
    96: 97,
    98: 99,
    100: 101,
    102: 103,
    104: 105,
    108: 463,
    109: 110,
    111: 464, 112: 464,
    440: 242, 113: 242,
    114: 465,
    116: 230, 117:230,
    118: 119,
    120: 121,
    439: 122,
    123: 212,
    238: 124,
    239: 466, 125: 466,
    240: 467, 126: 467,
    129: 130,
    137: 474, 233: 474,
    138: 139,
    140: 141,
    446: 143,
    147: 149, 148: 149,
    152: 154, 153: 154,
    155: 157, 156: 157,
    158: 160, 159: 160,
    161: 162,
    163: 164,
    165: 166,
    167: 168,
    170: 171,
    175: 468, 176: 468,
    177: 178,
    179: 181, 180: 181,
    298: 184, 183: 184,
    438: 185,
    187: 189, 188: 189,
    190: 424,
    191: 192,
    193: 469,
    194: 195,
    198: 430,
    200: 429,
    360: 202,
    204: 205,
    207: 472,
    209: 210,
    215: 461,
    216: 217,
    218: 219,
    220: 473, 221: 473,
    223: 224,
    458: 226,
    228: 229,
    231: 232,
    246: 248, 247: 248,
    252: 254, 253: 254,
    255: 257, 256: 257,
    258: 260, 259: 260,
    261: 262,
    263: 264,
    266: 267,
    268: 269,
    270: 272, 271: 272,
    273: 275, 274: 275,
    276: 277,
    278: 279,
    280: 281,
    283: 284,
    285: 286,
    287: 289, 288: 289,
    293: 295, 294: 295,
    296: 297,
    299: 476,
    300: 301,
    304: 306, 305: 306,
    307: 308,
    309: 311, 310: 311,
    406: 407, 315: 407,
    316: 317,
    318: 319,
    320: 321,
    322: 323,
    325: 326,
    328: 330, 329: 330,
    331: 332,
    333: 334,
    339: 340,
    341: 342,
    343: 344,
    345: 346,
    347: 348,
    349: 350,
    353: 354,
    355: 477, 356: 477,
    433: 358,
    363: 365, 364: 365,
    371: 373, 372: 373,
    374: 376, 375: 376,
    387: 389, 388: 389,
    390: 392, 391: 392,
    393: 395, 394: 395,
    396: 398, 397: 398,
    399: 400,
    401: 402,
    403: 405, 404: 405,
    408: 409,
    410: 411,
    415: 416,
    418: 419,
    420: 421,
    422: 423,
    425: 426,
    427: 428,
    431: 432,
    434: 435,
    436: 437,
    443: 445, 444: 445,
    447: 448,
    449: 450,
    451: 452,
    453: 454,
    456: 457,
    459: 460,
    495: 497, 496: 497,
    498: 500, 499: 500,
    501: 503, 502: 503,
    504: 505,
    506: 508, 507: 508,
    509: 510,
    511: 512,
    513: 514,
    515: 516,
    517: 518,
    519: 521, 520: 521,
    522: 523,
    524: 526, 525: 526,
    527: 528,
    529: 530,
    532: 534, 533: 534,
    535: 537, 536: 537,
    540: 542, 541: 542,
    543: 545, 544: 545,
    546: 547,
    548: 549,
    551: 553, 552: 553,
    554: 555,
    557: 558,
    559: 560,
    562: 563,
    564: 565,
    566: 567,
    568: 569,
    570: 571,
    572: 573,
    574: 576, 575: 576,
    577: 579, 578: 579,
    580: 581,
    582: 584, 583: 584,
    585: 586,
    588: 589,
    590: 591,
    592: 593,
    595: 596,
    597: 598,
    599: 601, 600: 601,
    602: 604, 603: 604,
    605: 606,
    607: 609, 608: 609,
    610: 612, 611: 612,
    613: 614,
    616: 617,
    619: 620,
    622: 623,
    624: 625,
    627: 628,
    629: 630,
    633: 635, 634: 635,
    636: 637,
    650: 652, 651: 652,
    653: 655, 654: 655,
    656: 658, 657: 658,
    659: 660,
    661: 663, 662: 663,
    664: 666, 665: 666,
    667: 668,
    669: 671, 670: 671,
    672: 673,
    674: 675,
    677: 678,
    679: 681, 680: 681,
    682: 683,
    684: 685,
    686: 687,
    688: 689,
    690: 691,
    692: 693,
    694: 695,
    696: 697,
    698: 699,
    704: 706, 705: 706,
    708: 709,
    710: 711,
    712: 713,
    714: 715,
    722: 724, 723: 724,
    725: 727, 726: 727,
    728: 730, 729: 730,
    731: 733, 732: 733,
    734: 735,
    736: 738, 737: 738,
    739: 740,
    742: 743,
    744: 745,
    747: 748,
    749: 750,
    751: 752,
    753: 754,
    755: 756,
    757: 758,
    759: 760,
    761: 763, 762: 763,
    767: 768,
    769: 770,
    772: 773,
    782: 784, 783: 784}
    pokimones = [] #list of pokemon numbers generated by generar_pokimons
    orden = []
    turnos = []
    def accion():
    global jugadores
    quienes_juegan_geteado = quienes_juegan.get().split()

    # Add items from variablesss to the existing jugadores dictionary
    for nombre in quienes_juegan_geteado:
    jugadores[nombre] = []
    print(jugadores)
    # Shuffle the jugadores dictionary for random order
    jugadores = dict(random.sample(list(jugadores.items()), len(jugadores)))

    # Create a fixed order for players based on the shuffled dictionary
    global orden
    orden = list(jugadores.keys())

    print(jugadores) # Just to test if it works
    generar_pokimones()
    botoncitos(elecciones, pokimones)
    elecciones.tkraise() # goes to the next page
    def botoncitos(elecciones, pokimones): # creates 1 button per pokemon generated and places them in a grid of 6
    def crear_boton(index):
    def seleccionar_pokemon_evoluto():
    nonlocal current_player # Declare current_player as non-local
    item = pokimones[index]
    item = pypokedex.get(dex=index)
    # Add the selected Pokemon to the player's list
    jugadores[current_player].append(item.name)
    # Disable the button and update the textbox
    button.config(state=DISABLED)
    textbox.insert(END, f"{current_player} elige a {item.name}\n")
    def seleccionar_pokemon():
    nonlocal current_player # Declare current_player as non-local
    item = pokimones[index]
    item = pypokedex.get(dex=index)
    # Add the selected Pokemon to the player's list
    jugadores[current_player].append(item.name)
    # Disable the button and update the textbox
    button.config(state=DISABLED)
    textbox.insert(END, f"{current_player} elige a {item.name}\n")

    # Update the current player using the fixed order
    current_player = orden[(orden.index(current_player) + 1) % len(orden)]
    item = pokimones[index]
    if item in evoluciones:
    for key, value in evoluciones.items():
    p = pypokedex.get(dex=key)
    e = pypokedex.get(dex=value)
    button = Button(elecciones, text=f"{p.name}({e.name})".title(), font=("Arial",12, "bold"), command=seleccionar_pokemon_evoluto)
    button.grid(sticky="nsew", row=index // 6, column=index % 6)
    else:
    item = pypokedex.get(dex=index)
    button = Button(elecciones, text=item.name.title(), font=("Arial",12, "bold"), command=seleccionar_pokemon)
    button.grid(sticky="nsew", row=index // 6, column=index % 6)
    def imprimir_equipos():
    textbox.delete(1.0, END) # Clear previous text
    for jugador, pokemones in jugadores.items():
    textbox.insert(END, f"{jugador}: {', '.join([pokemon.name for pokemon in pokemones])}\n") # Use pokemones list directly

    for index, item in enumerate(pokimones):
    print("hasta aqui funciona")
    crear_boton(index)
    # Empty textbox above the buttons
    # Initialize the current player using the fixed order
    current_player = orden[0]

    textbox = Text(elecciones, height=30, width=100)
    textbox.insert(END, f"El orden es {jugadores}\n")
    textbox.grid(row=8, column=0, columnspan=6)
    boton_final = Button(elecciones, text="Finalizar", command= lambda: imprimir_equipos())
    boton_final.grid(row=9, column=0)
    def generar_pokimones():
    pokemones= []
    numero_pokimons = len(jugadores)* 6 #number of pokemons determined by the number of players
    #if you want to play with legendary pokemons
    legendarios = False
    legendary_list = [144,145,146,150,151,243,244,245,249,250,251,377,378,379,380,381,382,383,384,385,386,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,638,639,640,641,642,643,644,645,646,647,648,649,716,717,718,719,720,721,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809]
    #loop
    while numero_pokimons != 0:
    poke = random.randint(1,809)
    if poke in pokemones:
    continue
    if legendarios == False:
    if poke in legendary_list:
    continue
    else:
    pokemones.append(poke)
    numero_pokimons = numero_pokimons-1
    pokemones = sorted(pokemones)#sort
    pokimones.extend(pokemones) #adds the list to the global variable pokimones

    #1st page
    inicio = Frame(root)
    inicio.grid(row=0, column=0)
    ttl = Label(inicio, text="¿Quienes juegan?")
    quienes_juegan = StringVar()
    txt1 = Entry(inicio, textvariable = quienes_juegan, width=50) #who plays?
    btn1 = Button(inicio, text="Confirmar", command= lambda: accion()) #turns to the 2nd page, elecciones


    ttl.grid(padx=650)
    txt1.grid(pady=20)
    btn1.grid()

    elecciones = Frame(root) #the second page, where it displays 6 buttons with pokemon per user
    elecciones.grid(row=0, column=0)


    inicio.tkraise()

    root.mainloop()


    its supposed to check if the number has an evolution or not. If it has an evolution, it creates a button that displays "pokemon(evolution)". If not, it creates a button with the pokemon name. Right now the program stops when it tries to create the buttons, there is no error code in the terminal. I ran it in the debugger and the problem seems to be that while checking in the dictionary "evoluciones", even if it finds the key, it continues past it and it stops working after x number of loops. i have no idea how to fix that, so im open to any other way to implement what i was trying to do. Previously, the buttons only had the pokemon name, and everything worked fine. The code in that section was this:

    def crear_boton(index):
    def seleccionar_pokemon():
    nonlocal current_player # Declare current_player as non-local
    item = elegibles[index]
    # Add the selected Pokemon to the player's list
    jugadores[current_player].append(item)
    # Disable the button and update the textbox
    button.config(state=DISABLED)
    textbox.insert(END, f"{current_player} elige a {item.name}\n")

    # Update the current player using the fixed order
    current_player = orden[(orden.index(current_player) + 1) % len(orden)]
    item = elegibles[index]
    button = Button(elecciones, text=item.name.title(), font=("Arial",12, "bold"), command=seleccionar_pokemon)
    button.grid(sticky="nsew", row=index // 6, column=index % 6)
    def imprimir_equipos():
    textbox.delete(1.0, END) # Clear previous text
    for jugador, pokemones in jugadores.items():
    textbox.insert(END, f"{jugador}: {', '.join([pokemon.name for pokemon in pokemones])}\n") # Use pokemones list directly

    elegibles = [pypokedex.get(dex=dex_number) for dex_number in pokimones] # transforms the numbers generated into pokemon names
    for index, item in enumerate(elegibles):
    crear_boton(index)


    apologies for any mistake in grammar. English its not my first language

    Continue reading...

Compartilhe esta Página