I have a program that prints multiplication tables.
def print_tables(input):
for i in xrange(1,11):
print "%s x %s = %s" %(input, i, input*i)
user_input = raw_input("What do you want multiplied ten fold? ")
if(user_input.isdigit()):
print_tables(int(user_input))
else:
print_tables(user_input)
If the user enters a string "a", I would expect the output to be:
a x 1 = a
a x 2 = aa
a x 3 = aaa
a x 4 = aaaa
a x 5 = aaaaa
a x 6 = aaaaaa
a x 7 = aaaaaaa
a x 8 = aaaaaaaa
a x 9 = aaaaaaaaa
a x 10 = aaaaaaaaaa
Calling the print_tables function in both the if and else blocks does feel a bit redundant to me.
Is there a better way in Python to call the print_tables function regardless of the parameter type?
1 Answer