猿问

使用 if 语句打印所有选项的递归函数

我希望我的函数打印所有选项,如下所示:


year: 2017 month: 1

year: 2017 month: 2

year: 2017 month: 3

year: 2018 month: 1

year: 2018 month: 2

year: 2018 month: 3

我写了这段代码:


years = [2017, 2018]

years_index = 0

month = 1

def parse():

    global years

    global years_index

    global month

    print(str('year: ' + str(years[years_index])) + ' month: ' + str(month))   

    if years_index < len(years) -1:

        if month < 3:

            month +=1

            parse()

        else:

            years_index +=1

            month = 1

            parse()      

parse()

我的代码打印这个:


year: 2017 month: 1

year: 2017 month: 2

year: 2017 month: 3

year: 2018 month: 1

我做错了什么?


猛跑小猪
浏览 94回答 3
3回答

月关宝盒

这里不需要递归:from itertools import productyears = [2017, 2018]months = [1, 2, 3]for year, month in product(years, months):&nbsp; &nbsp; print(f'year: {year} month: {month}')因为 Python 缺乏尾递归优化并且调用用户定义函数的成本相对较高,所以递归只应在比循环更清晰的情况下使用。

Cats萌萌

是这样的:years = [2017, 2018]years_index = 0month = 1def parse():&nbsp; &nbsp; global years&nbsp; &nbsp; global years_index&nbsp; &nbsp; global month&nbsp; &nbsp; print(str('year: ' + str(years[years_index])) + ' month: ' + str(month))&nbsp; &nbsp; if month < 3:&nbsp; &nbsp; &nbsp; &nbsp; month +=1&nbsp; &nbsp; &nbsp; &nbsp; parse()&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; years_index +=1&nbsp; &nbsp; &nbsp; &nbsp; month = 1&nbsp; &nbsp; &nbsp; &nbsp; if years_index >= len(years):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; parse()parse()我建议您重构此代码并使用循环而不是递归。

一只名叫tom的猫

我试着在递归函数上做一些 OOP 来找点乐子。这是我解决它的方法,希望你能觉得它有用。基本上解决它需要在第一个条件中添加一个小于或等于,当它完成时,为了避免 IndexError,我使用了一个 catch,因为它是预期行为的一部分。class Example(object):&nbsp; &nbsp; #Define variables on an object level&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; self.years = [2017, 2018]&nbsp; &nbsp; &nbsp; &nbsp; self.years_index = 0&nbsp; &nbsp; &nbsp; &nbsp; self.month = 1&nbsp; &nbsp; def parse(self):&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(str('year: ' + str(self.years[self.years_index])) + ' month: ' + str(self.month))&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Had to add an equal sign to make it work on the next year, otherwise it would stop here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.years_index <= len(self.years) -1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.month < 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.month +=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.years_index +=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.month = 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.parse()&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; &nbsp; &nbsp; #The catch block is used when the index gets higher than the list's length. So that it does not&nbsp; &nbsp; &nbsp; &nbsp; #cause an error, because it is the intended behavior for me.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; except Exception as e:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('Ran out of years');#Calling functionexample = Example()example.parse()
随时随地看视频慕课网APP

相关分类

Python
我要回答