zoukankan      html  css  js  c++  java
  • py-day3-2 python 函数递归

    # 递归
    def calc(n):
        print(n)
        if int(n/2) == 0:
            return n
        res = calc(int(n/2))
        return res
    res = calc(10)
    print(res)
    
    10
    5
    2
    1
    1

    import time
    
    person_list = ['小马','小李','小何','小张','百度','小黄']
    def ask_way(person_list):
        print('-'*60)
        if len(person_list) == 0:
            return '没听说过,不知道。'
        person=person_list.pop(0)
        if person == '百度':
            return '%s说:坐地铁9号线到九亭下就可以了。'%person
    
        print('请问[%s], 你知道九亭在哪吗?'%person)
        print('%s回答道:我不知道,我问下我的朋友%s··'%(person,person_list))
        time.sleep(3)
        res=ask_way(person_list)
        print('%s问的结果是:%res' %(person,res))
        return res
    res=ask_way(person_list)
    print(res)
    
    请问[小马], 你知道九亭在哪吗?
    小马回答道:我不知道,我问下我的朋友['小李', '小何', '小张', '百度', '小黄']··
    ------------------------------------------------------------
    请问[小李], 你知道九亭在哪吗?
    小李回答道:我不知道,我问下我的朋友['小何', '小张', '百度', '小黄']··
    ------------------------------------------------------------
    请问[小何], 你知道九亭在哪吗?
    小何回答道:我不知道,我问下我的朋友['小张', '百度', '小黄']··
    ------------------------------------------------------------
    请问[小张], 你知道九亭在哪吗?
    小张回答道:我不知道,我问下我的朋友['百度', '小黄']··
    ------------------------------------------------------------
    小张问的结果是:'百度说:坐地铁9号线到九亭下就可以了。'es
    小何问的结果是:'百度说:坐地铁9号线到九亭下就可以了。'es
    小李问的结果是:'百度说:坐地铁9号线到九亭下就可以了。'es
    小马问的结果是:'百度说:坐地铁9号线到九亭下就可以了。'es
    百度说:坐地铁9号线到九亭下就可以了。
  • 相关阅读:
    【Python】【文件】查找指定路径中是否存在目标文件(含此路径下的子文件夹)
    时隔一年的2020noip
    nacos 笔记
    webflux 小例子
    spring Initializr 笔记
    临时~spring启动过程
    Mac通过crontab设置定时任务报错Operation not permitted
    Isolation Forest Implementation(孤立森林)
    let arr=['a'] JSON.stringify(arr) 输出:“['a']” let arr2 = “['a']” JSON.parse(arr2) 输出: ['a']
    js对象中key值加引号和不加引号的区别
  • 原文地址:https://www.cnblogs.com/majunBK/p/10428210.html
Copyright © 2011-2022 走看看