0

This is my first question on Stack. I hope the explanation is understandable, and the posts' rules respected.

Problem : I try to create a display of 12 multiplication tables, 5 by 5. The code runs well, but it never stops. It seems to be a problem in the syntax of the while loop.

What I tried :

  • changing the while condition with :
while lot_tab != [0]: # Stop quand vide.
while lot_tab != [""]: # Stop quand vide.

Nothing works, I don't understand. So I'm asking for your help.

Code :

LIGNE_TMUL = "%2i x %2i = %3i "
nmax = 12

tab_par_lig = 5
tab_total = range(1, nmax + 1) # Soit ici 12.
lot_tab = tab_total[:tab_par_lig] # Prends un lot de 5 pour l'affichage.
tab_total = tab_total[tab_par_lig:] # Les enleve du stock restant.

# while tab_total != []: # Test
while lot_tab != []: # Stop quand vide.
    for x in range(1, nmax + 1): # traîter les nombres de 1 à 12.
        accumul = [] # Stock 1 ligne d'une table.
        for y in lot_tab: # Construit les colonnes.
            accumul.append(LIGNE_TMUL%(y, x, x * y)) # Stock une ligne dans accumul[]
        print("".join(accumul)) # Affiche la ligne créée avec y
    print("\n") # Separation verticale entre blocs de tables.
    lot_tab = tab_total[:tab_par_lig] # Prends un lot de 5 pour l'affichage.
    tab_total = tab_total[tab_par_lig:]
  • 1
    Welcome to Stack Overflow. Did you try to figure out what `lot_tab` *actually is equal to*, at the point where you want the code to stop? It seems that you intend for the loop to exit when `lot_tab` is empty. In your own words, *why should it ever become empty*? – Karl Knechtel May 03 '22 at 18:27
  • "I try to create a display of 12 multiplication tables, 5 by 5. " I can't understand what this means. What should the result look like? – Karl Knechtel May 03 '22 at 18:28
  • Ah, I think I see the problem. It appears that you are expecting `range` to create a `list` object, which will eventually become an empty list if you repeatedly slice away the first few elements. `range` **does not** create a `list` in 3.x, it is its own type. – Karl Knechtel May 03 '22 at 18:29
  • @KarlKnechtel, thanks for the welcome. Your question made me ask the value of lot_tab at the end of the "for x [...]" loop. And it showed that it value is range(13, 13). So by changing " while lot_tab != range(13, 13) " it works. Thanks for your help ! – Yellow_patience May 03 '22 at 18:38

0 Answers0