Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
level3-lesson14-diy1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
027f59a4
authored
3 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
0f53c1f3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
1.py
1.py
0 → 100644
View file @
027f59a4
'''
0.创建递归函数
1.输入一个num 并转换成整数
2.输出调用递归函数的结果
递归函数会不断的调用函数本身 在递的操作下调用本身
num = 3
1 + 2 + 3
从3开始去加
递归条件:
num != 1 num > 1
基线条件:
num == 1
return 返回
在递归条件下 进行的操作是递
return 函数名(num-1)+num
在基线条件下 进行的操作是归
return 1
在主函数中调用函数 实参为输入的num 3 主程序中 得到了1+2+3的结果
在函数中的第一次调用 实参是(num-1)2 num = 3 num + 函数名(num-1) 1+2
在函数中的第二次调用 实参是(num-1)1 num = 2 num + 函数名(num-1)
在函数中的第三次调用 1 == 1 不符合往下递归的条件开始返回
'''
# def func(n):#创建一个函数func,形参是n
# #判断是否符合递归条件
# if n > 1:
# #在递归条件下要返回的内容
# return func(n-1)+n
# #判断是否符合基线条件
# if n == 1:
# #返回1
# return 1
# #输入一个整数num
# num = int(input("输入一个数字:"))
# #输出调用函数func
# print(func(num)).
def
func
(
n
):
#创建一个函数func,形参是n
#判断是否符合递归条件
#判断是否符合基线条件
if
n
==
1
:
#返回1
return
1
#在递归条件下要返回的内容
return
func
(
n
-
1
)
+
n
#输入一个整数num
num
=
int
(
input
(
"输入一个数字:"
))
#输出调用函数func
print
(
func
(
num
))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment