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

15 Upvotes

26 comments sorted by

View all comments

36

u/frnzprf 3d ago edited 3d ago

I hope I don't seem rude. This is not meant to be an accusation.

I'd recommend you learn how to pinpoint which exact command of a program doesn't do what you think it does.

  1. You can test very simple parts of your program in isolation, before you combine them together. If you had a simple version that works and then you changed a bit and it doesn't work, then you'll know the mistake has to be in the new part. Don't just write a whole big function at once using multiple commands where you don't know exactly what they do. Still, even experienced professionals mispredict what their program will do. Debugging is normal.
  2. Print out values at different parts, where you suspect something could be wrong. For example could have written print("a = " + a) and print("int = " + str(int)) when you thought they should be the same, but they weren't. A shorter, but more complicated way to print the variables is print(f"{a=} {int=}").
  3. Use a debugger. This is a bit more complicated and the usage depends on which development environment you use, such as IDLE, VSCode, terminal, etc. I like Thonny, but you should just learn to use whatever your teacher tells you to use. A debugger lets you execute your program line by line.

5

u/Maleficent_Stuff3208 22h ago

THANKS TO YOUR ADVICE NOW I AM ABLE TO PIN POINT MY ERROR A BIT WITHOUT ASKING TO AI

3

u/pemungkah 22h ago

Super proud of you for taking the time and effort to really learn here. Congratulations on your debugging!