带有递归的Python时间表代码

带有递归的Python时间表代码,python,recursion,Python,Recursion,我必须使用递归函数生成一个时间表代码。我必须要求用户提供一个数字,并打印出从1到12的时间表。我必须使用递归函数,不允许对循环或使用,而循环和除用户输入之外的所有变量必须在函数内定义。我无法定义用户提供的数字需要乘以的数字。E.X.2X12X22X3 def times_tables(num): def multiply(x): product = x * num if x < 12: print (str(multiply(x + 1)))

我必须使用递归函数生成一个时间表代码。我必须要求用户提供一个数字,并打印出从1到12的时间表。我必须使用递归函数,不允许对循环或
使用
,而
循环和除用户输入之外的所有变量必须在函数内定义。我无法定义用户提供的数字需要乘以的数字。E.X.2X12X22X3

def times_tables(num):
    def multiply(x):
        product = x * num
    if x < 12:
        print (str(multiply(x + 1)))

user = input("Enter a number: ")
times_tables(user)
def times_表(num):
def倍增(x):
乘积=x*num
如果x<12:
打印(str(乘(x+1)))
用户=输入(“输入一个数字:”)
时间表(用户)

如果我在
times_tables
函数中定义
x
,那么每次函数运行时,它都会返回到我第一次设置的值。谢谢你的帮助

我不确定我是否理解您的任务,但以下是我的尝试:

def timetable(n, time=1):
    if time <= 12:
        print(n, time, n*time)
        timetable(n, time+1)
    else:
        return

timetable(int(input('Number: ')))
def时间表(n,时间=1):

如果时间不修改x,则x通过值传递,这意味着它被复制

如果您想将退出条件保持在递归之外,您需要一种直接从递归写入X的方法,这可能会涉及一个全局(避免使用错误的做法)

您需要在乘法中包含exit条件,因为这将是您的递归,在这种情况下,您的X将增加,您将检查正确的递增值。或者像ruggfrancesco建议的那样一起更改函数

当我(图像)搜索“times table”时,我得到的结果与其他答案产生的结果非常不同。下面是我的二维递归解决方案:

def times_tables(n, t=1):
    if t == 13:
        return
    print(str(n) + " x " + str(t) + " = " + str(n*t))
    times_tables(n, t+1)

times_tables(int(input("Enter number: ")))

Enter number: 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36
def times_table(limit):
    number_format = "{{:{}}}".format(len(str(limit ** 2)))

    def times_table_recursive(number, increment):
        minimum = max(increment, 1)

        if number <= minimum * limit:
            print(number_format.format(number if number > 0 else 'x'), end=' ')
            times_table_recursive(number + minimum, increment)
        elif increment < limit:
            print()
            increment += 1
            print(number_format.format(increment), end=' ')
            times_table_recursive(increment, increment)
        else:
            print()

    times_table_recursive(0, 0)

times_table(12)

只上升到
时间\u表(30)
,而不扩展Python的递归深度限制。

如果我能很好地回忆递归,我认为您必须设置条件。从基本情况开始。最简单的步骤。然后让下一步(第二个最简单的步骤)引用最简单步骤的结果。从这里开始,你永远不会修改x,你不会从乘法(x)中返回任何东西,而这个答案可能是正确的。只有代码的答案很少有帮助。请评论您的代码并解释此代码如何解决问题。此答案不正确。第5行缺少括号使其无法运行。修复后,如果无法将参数正确传递给递归调用,则会导致其堆栈溢出。它对
fun()
的结果进行乘法运算,结果总是返回
None
。完全失策。
def times_table(limit):
    number_format = "{{:{}}}".format(len(str(limit ** 2)))

    def times_table_recursive(number, increment):
        minimum = max(increment, 1)

        if number <= minimum * limit:
            print(number_format.format(number if number > 0 else 'x'), end=' ')
            times_table_recursive(number + minimum, increment)
        elif increment < limit:
            print()
            increment += 1
            print(number_format.format(increment), end=' ')
            times_table_recursive(increment, increment)
        else:
            print()

    times_table_recursive(0, 0)

times_table(12)
> python3 test.py
x     1   2   3   4   5   6   7   8   9  10  11  12 
  1   1   2   3   4   5   6   7   8   9  10  11  12 
  2   2   4   6   8  10  12  14  16  18  20  22  24 
  3   3   6   9  12  15  18  21  24  27  30  33  36 
  4   4   8  12  16  20  24  28  32  36  40  44  48 
  5   5  10  15  20  25  30  35  40  45  50  55  60 
  6   6  12  18  24  30  36  42  48  54  60  66  72 
  7   7  14  21  28  35  42  49  56  63  70  77  84 
  8   8  16  24  32  40  48  56  64  72  80  88  96 
  9   9  18  27  36  45  54  63  72  81  90  99 108 
 10  10  20  30  40  50  60  70  80  90 100 110 120 
 11  11  22  33  44  55  66  77  88  99 110 121 132 
 12  12  24  36  48  60  72  84  96 108 120 132 144 
>