**Python code to find bugs (6) **
**Code design requirements of the previous period: **
A ball falls freely from a height of 100 meters, and rebounds back to half of its original height each time it hits the ground. When it falls again, how many meters does it pass when it hits the ground for the tenth time? How high is the 10th rebound?
code show as below:
Please read the code carefully to find out the bugs!
**Correct answer: **There are 2 bugs. (For the answer time, see how many answers are correct?)
(1) It is still the parameter of the range() method, which determines the number of for loops. The starting number here is 1, so if t=10 and the number of bounces is 10, should the number of for loops also be 10? So, what should be the second parameter of range? 11 right? So, here, t should be t+1. Haha, are you still checking the memory of some students? !
(2) h/=2, is this position a bit strange? Although Python is strictly indented, is the indentation really correct? If this is correct, what does it mean? Does it mean that the height of the first bounce is constant? Haha, the second ball is extraordinary, is it an AI ball? If not, then its indentation should be moved forward one level, so as to ensure that the height of each bounce (sentence loop) is reduced by half.
So, the correct code should look like this:
Well, this one looks more "pure"!
**Comment: **The main purpose of finding bugs yesterday:
(1) Once again consolidate the understanding of the range() parameter.
(2) Correctly understand and use indented grammar
By the way, yesterday we also mentioned an interesting question about the code genes of python. It seems that we prefer to use sequence type data structures such as list lists. Here we list the code of another solution that is more traditional (other languages), for everyone to appreciate, taste the difference between the two styles, and see if the influence of genes is very strong?
For students who have difficulty reading the above code, please read and study the "Introduction to Python" sent by Gaodu, or the "Introduction to Python" video course on the Gaodu website.
Today’s question is announced below.
**Code design requirements for this issue: **
Problem with monkey eating peaches: The monkey picked off several peaches on the first day, and ate half of it immediately, but it was not enough, and ate one more. The next morning, he ate half of the remaining peaches and ate another one. After that, I ate half and one of the remaining half of the previous day every morning. When I wanted to eat again in the morning of the 10th day, I saw that there was only one peach left. How many were picked on the first day?
**Demand analysis: **To solve this kind of problem, there is actually an obvious entrance, that is, the result is known: at the end (the 10th day) only 1 peach is left. Therefore, we will adopt the method of reverse thinking, inferring from the back to the front.
code show as below:
**Brother, you who are observant of everything, see where the bug is? **
Find it out, post it in the message, and have the answer tomorrow.
For students who cannot find a bug, it is recommended to review the "Introduction to Python" published earlier outside the Gaodu number, as well as the "Easy Introduction to Python" and "Python Web Development" video courses on the Gaodu website.
https://www.igaodu.cn (Love Gaodu, learn programming, teach you step by step!)
Reminder, pay special attention to the details of the format grammar.
**The correct answer will be announced tomorrow. **
Reminder: Conventionally, all codes are based on Pythpn3.
Recommended Posts