Function problem in python[Multiplication table]

I’ve to write a function that should print multiplication table of number X .That means if i input 5 then, this function should print the multiplication table of 5.
Like that,

 5 x 1 = 5
 5 x 2 = 10
 5 x 3 = 15
 5 x 4 = 20
 5 x 5 = 25
 5 x 6 = 30
 5 x 7 = 35
 5 x 8 = 40
 5 x 9 = 45
 5 x 10 = 50
 5 x 11 = 55
 5 x 12 = 60

But i have to remember the condition that,if i don’t input anything then it should print the multiplication table of 1

Here is my code,

def namata(m):
    if m == False:
        for i in range(10):
            print(1,"x",i+1,"=",1*(i+1))
    else:
        for i in range(10):
            print(m,"x",i+1,"=",m*(i+1))

number = int(input())
print(namata(number))

i did first part correctly, but how i’ll show multiplication table of 1,when function don’t take anything as input?

You can use try and except to check for input i.e.

def namata(m):
    for i in range(10):
        print(m,"x",i+1,"=",m*(i+1))
try:
    number=int(input())
    namata(number)
except:
    namata(1)

1 Like

wowao,it’s working!Thank you so much Fatma Nagori :heart:
But why i should use this,

i didn’t get that.Can you please explain?

when I was checking the code I have added that condition for testing and I forget to remove that line .but it will not effect the result because i value will be never equals to 10 .

1 Like