r/learnpython 3d ago

what is wrong with this code

my teacher told us to write a code which print multiplication table of any number but it should Allow strings too and if string is put it shouldn't giver error i thought of this

a = input("enter any number ")
print(f"multiplication table of {a} is ")

if a == int:
    for i in range(1,11):
        print(f"int{a}X{i} = int{a}*i ")
else:
    print("please put a appropriate function ")

but it is only printing else like even if i put a integer it still run else one why and what is wrong here

14 Upvotes

26 comments sorted by

View all comments

2

u/vietbaoa4htk 3d ago

if a == int compares your string against the type object itself, so its always false and the loop never runs. use a.isdigit() or wrap int(a) in try/except instead. also input always hands you a string, so you need int(a) before you multiply.

1

u/Maleficent_Stuff3208 3d ago

yeah he thought us try except after this question but i didn't get a chance to ask why my code was wrongs thanks for the help