如何通过定义函数显示乘法表

时间:2018-07-04 11:45:24

标签: python-3.x

我知道如何使用循环显示乘法表,但是我如何制作一个可以执行相同操作的函数?请告诉我整个程序

1 个答案:

答案 0 :(得分:0)

#In this method the value of the highest multiple is passed in mult
#base is base value which will be multiplied
#this function runs from mult to 1
def multiply_rev(base,mult):
    if(mult == 0):
        return
    else:
        print("{} * {} = {}".format(base,mult,base*mult))
        multiply_rev(base,mult-1)


#In this method the value of the lowest multiple is passed in mult
#base is base value which will be multiplied
##this function runs from mult to 10
def multiply_for(base,mult):
    if(mult == 11):
        return
    else:
        print("{} * {} = {}".format(base,mult,base*mult))
        multiply_for(base,mult+1)        

multiply_rev(2,10)
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++")
multiply_for(2,1)

这是递归的典型示例。 Google递归以获取更多信息。