Python| function using recursion to solve

Problem Description

If a ball falls freely from a height of 100 meters, it bounces back to half of its original height each time it hits the ground, and then falls again. How many meters does it pass when it hits the ground for the 10th time? How high is the 10th rebound?

solution

First, analyze the problem and calculate its value according to the problem with a mathematical geometric sequence. From the problem, it can be known that the problem function can be solved with a recursive function. First, use the function definition symbol def to customize a new function, and use the row recursive function to repeat the input value Loop, and then use the for loop to assign a value to the number of drops of the ball in the question, get the tenth drop value, and then use return to return the obtained value and output the previously defined function value. Still need to calculate sums, when judging the return value, pay attention to whether the function value to be printed meets the definition of recursive function.

Code example:


def row(n, sums, height):#def is a symbol to define a new function,row means that this function is a recursive function.if n ==10:return sums
  print(sums, height)returnrow(n+1, sums+(height*2),  height/2) # row()Represents returning the value in the recursive function to the output.print(row(1,100,50))
1005020025.0250.012.5275.06.25287.53.125293.751.5625296.8750.78125298.43750.390625299.218750.1953125299.609375

Conclusion

Learn to master the calculation methods in python functions, use recursive functions to solve problems, and be familiar with the use of if conditions in python. Learn the meaning of functions returned in python functions.

END

Chief Editor | Wang Nanlan

Responsible Editor | Shen Zhijian

**The stronger the ability, the greater the responsibility. Seek truth from facts, rigorous and meticulous. **

** ——Where2go team**

Recommended Posts

Python| function using recursion to solve
Using Python to implement multiple clipboards
How to solve python dict garbled
How to run id function in python
How to debug python program using repr
linux+ubuntu solve python
01. Introduction to Python
Python enumerate() function
Introduction to Python
Python function buffer
How to use the round function in python
How to use the zip function in Python
How to use the format function in python
How to switch the hosts file using python
Detailed examples of using Python to calculate KS
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python custom function basics
Join function in Python
Python built-in function -compile()
Python function basic learning
Python data analysis-apply function
Python3 built-in function table.md
Hyperparameter optimization using Python
Using Python to analyze the Guangzhou real estate market
Python Print print timer function
Python defines a function method
Centos default python2.6 upgrade to
Solution to python alignment error
Python high-order function usage summary!
CentOS upgrade python2 to pyth
Python code to find bugs (2)
Python code to find bugs(7)
How to comment python code
Python realizes online translation function
Python code to find bugs(4)
Python code to find bugs (3)
Python code to find bugs(9)
How to learn python quickly
How to uninstall python plugin
Introduction to Python related modules
Python tornado upload file function
Python magic function eval () learning
Python code to find bugs(6)
How Python implements FTP function
Python code to find bugs (1)
Python code to find bugs(8)
3 ways to encrypt Python files
How to understand python objects
Python code to find bugs(5)
How to use python tuples
Python implements image stitching function
Python high-order function usage summary!