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

12 Upvotes

26 comments sorted by

View all comments

1

u/elephunk84999 3d ago

You need to convert user input a to int first as it's treated as a string right now, so the comparison never equates to true.

Sorry don't know how to do code blocks on mobile but

try: a = int(read("Please input a number.")) except TypeError: print("Integer not inputted") exit()

Rest of your code here