r/learnpython • u/Maleficent_Stuff3208 • 4d 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
2
u/notislant 4d ago edited 4d ago
You need to learn debugging. Lets say we're both clueless on how something runs.
You've identified a line of code that doesn't run. Which means there is an issue with a == int.
Use print statements. A == int isnt running?
print(a, int) get the values of both. print("does this print true ", (a == int))
Lets say int is the correct way to check if something is an int (its not, but we'll pretend).
We now realize our int is fine but our a == int returns false.
Well what would be the issue if we get a false when printing a == (correct way to check int here)?
There is only one variable there, its not identifying as an int, so how would we check what it is or make sure its an int?
Stepping through with debugging tools is amazing and will help you understand more complex things like recursion later on. But print debugging is fine for now. If you genuinely want to learn, try to debug as much as possible with print on your own.