python函数可以递归调用吗,为什么我的Python函数不是递归的/调用自身的呢?

我正在做一个字符串重写函数,它接受一个字符串并根据字典中的规则重写它。代码只运行一次就可以了,但我需要它调用自己n次,并打印“nth”重写的字符串。我需要递归的代码是:S = "AB"

def srs_print(S, n, rules):

'''

A function that takes a dictionary as SRS rules and prints

the output

'''

axiom = list(S)

key = []

value = []

output = ''

for k in rules:

#Inputs the keys of the rules dictionary into a new list

key.append(k)

#Inputs the value of the rules dictionary into a new list

value.append(rules[k])

for x in axiom:

if x in key:

axiomindex = key.index(x)

output += value[axiomindex]

else:

output += x

S = output

return S

#j = srs_print(S, 5, {'A':'AB', 'B': 'A'})

#print(j)

#while len(range(n)) > 0:

# S = srs_print(S, n, rules)

# n = n-1

#print("The", n, "th rewrite is " )

#j = srs_print(S, 5, {'A':'AB', 'B': 'A'})

print(srs_print("A", 5, {'A':'AB', 'B': 'A'}))

这会将“A”变成“AB”,但我需要它将'S'放回函数中,然后再次运行'n'次。如您所见,一些注释代码是我尝试使用但失败的行。在

我希望我已经解释清楚了。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值