首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python,我对如何解决这个问题有几个想法,但是哪一个应该是正确的呢?

Python,我对如何解决这个问题有几个想法,但是哪一个应该是正确的呢?
EN

Stack Overflow用户
提问于 2022-07-01 03:20:25
回答 1查看 80关注 0票数 -1

我不需要帮助,这是非常容易的,我需要帮助来决定我应该使用哪几种方式。你觉得我能用递归吗?我至少需要一个

if

但是,上面写着

函数调用

当然,我正在考虑使用递归,但是我需要一个if-否则,但是它说“只有函数调用”。

考虑下面的Python函数,它输出4次来自著名计算机科学家的引用:

代码语言:javascript
复制
def print4x() :
  print("Controlling complexity is the essence of programming!")
 print("Controlling complexity is the essence of programming!")
 print("Controlling complexity is the essence of programming!")
 print("Controlling complexity is the essence of programming!")

在一个完整的Python程序中使用此print4x函数,该程序输出Kernighan引用64次。您的解决方案必须包含一个主函数、print4x函数和至少一个附加函数。你不应该用任何循环或者同时循环之类的-

只是函数调用!

简单地调用print4x 16次并不是一个可接受的解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-01 03:27:42

由于我们不能使用循环或条件语句,而且只能使用函数,所以这里是我的解决方案。

代码语言:javascript
复制
def print4x() :
    print("Controlling complexity is the essence of programming!")
    print("Controlling complexity is the essence of programming!")
    print("Controlling complexity is the essence of programming!")
    print("Controlling complexity is the essence of programming!")

def repeat(num):
    print4x()

numbers = (1, 2)
result = list(map(repeat, numbers))

在这里,我们只是在使用函数。是的,可以有一个改进的代码,我是开放的意见,在意见。

关于问题语言的探讨

如果我们严格地坚持问题的语言,它说只是函数调用

这使我们无法使用循环和条件语句,这使得使用递归变得困难。

但是,一些内置函数也可以作为循环工作(如map())。因此,这个问题需要详细说明哪些函数是允许的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72824054

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档