Python problem with variables I'm confused

Question:

I’m a beginner in python and I got a problem while doing a project for school, I have to create a multiplication table tool in a terminal. My code is going well but… My code looks like that :

print("1 - Afficher une table de multiplication")
print("2 - Afficher toutes les tables de multiplication")
print("3 - Interroger sur une table de multiplication")
print("4 - Interroger sur plusieurs tables de multiplication")
print("5 - Interroger sur toutes les tables de multiplication")
print("6 -  Quitter")


choix = input("Saisir votre choix = ")


if choix == "1":
table = input("Quelle table voulez vous afficher ? ")

if table == "1":
table=1
for i in range(11):
     resultat=table*i
     print(table," x ",i," = ",resultat)
     print("------------------------------")


if table == "2":
table=2
for i in range(11):
        resultat=table*i
        print(table," x ",i," = ",resultat)
        print("------------------------------")
        
if table == "3":
table=3
for i in range(11):
     resultat=table*i
     print(table," x ",i," = ",resultat)
     print("------------------------------")


if table == "4":
table=4
for i in range(11):
        resultat=table*i
        print(table," x ",i," = ",resultat)
        print("------------------------------")




if table == "5":
table = 5
for i in range(11):
     resultat=table*i
     print(table," x ",i," = ",resultat)
     print("------------------------------")


if table == "6":
table=6
for i in range(11):
        resultat=table*i
        print(table," x ",i," = ",resultat)
        print("------------------------------")
        
        
if table == "7":
table=7
for i in range(11):
     resultat=table*i
     print(table," x ",i," = ",resultat)
     print("------------------------------")


if table == "8":
table=8
for i in range(11):
        resultat=table*i
        print(table," x ",i," = ",resultat)
        print("------------------------------")
        
if table == "9":
table=9
for i in range(11):
     resultat=table*i
     print(table," x ",i," = ",resultat)
     print("------------------------------")


if table == "10":
table=10
for i in range(11):
        resultat=table*i
        print(table," x ",i," = ",resultat)
        print("------------------------------")


if choix == "2":
table = input("Voulez vous afficher toutes les tables ?")
for i in range(11):
        resultat=table*i
        print(table," x ",i," = ",resultat)
        print("------------------------------")

But when I chose the option 2 to display every tables, it just prints an error saying that table is not defined, and it’s because the condition "if choix == "1"" is not chosen, so table is not defined, but I want to display every tables and not one table, how could I do ?

Thank you guys

Asked By: Zarixz

||

Answers:

Let’s say the indentations are fine, table will be defined only if choix is equal to "1".

if choix == "1":
    table = input("Quelle table voulez vous afficher ? ")

table does not seem to be defined for its initial value in any other place before that, hope it helps.

Few general tips:

  • Indentation best practice is 4 spaces although it could be different value, just make it consistent.
  • You can use if-else in order to "save" redundant if checks
  • Make use of f-string, nicer to use for also faster.
Answered By: Paz Bazak

What Paz said. Try this for choice 2:

if choix == "2":
    table = [1,2,3,4,5,6,7,8,9,10]

    for j in range(len(table)):
        for i in range(11):
            resultat=table[j]*i
            print(table[j]," x ",i," = ",resultat)
            print("------------------------------")

        print('nn')

Also, might I suggest a slightly neater way of doing this?

choice = int(input('What is your choice: '))

if choice == 1:
    table = int(input('Which timetable do you want to see: '))
    for i in range(11):
        print(table," x ",i," = ",table*i)
        print("------------------------------")

if choice == 2:
    table = [1,2,3,4,5,6,7,8,9,10]
    for j in range(len(table)):
        for i in range(11):
            resultat=table[j]*i
            print(table[j]," x ",i," = ",resultat)
            print("------------------------------")
        print('nn')
Answered By: Maruf Talukdar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.