Python code to find bugs(6)

**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

Python code to find bugs (2)
Python code to find bugs(7)
Python code to find bugs(4)
Python code to find bugs (3)
Python code to find bugs(9)
Python code to find bugs(6)
Python code to find bugs (1)
Python code to find bugs(8)
Python code to find bugs(5)
Actually very simple | Python code to find bugs (12)
Minimalism is the soul of Python | Python code to find bugs (10)
How to comment python code
python how to view webpage code
How to wrap in python code
Python handles operation code to execl
01. Introduction to Python
200 lines of Python code to achieve snake
Python Romberg method to find integral examples
Does Python code need to be indented
Introduction to Python
How to use code running assistant in python
500 lines of python code to achieve aircraft war
How to set code auto prompt in python
Python how to move the code collectively right
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python SMS bombing code
Centos default python2.6 upgrade to
CentOS upgrade python2 to pyth
How to find the area of a circle in python
How to learn python quickly
Introduction to Python related modules
Python regular expression example code
Python implements code block folding
Python install OpenCV sample code
3 ways to encrypt Python files
Python implements verification code recognition
How to understand python objects
How to use python tuples
Is python code case sensitive
Python telnet login function implementation code
Python| function using recursion to solve
Method steps to increase python font
Use python to query Oracle database
Use C++ to write Python3 extensions
How to write python configuration file
Python novice learns to raise usage
Use python to achieve stepwise regression
How to save the python program
Python is a cross-platform language code
Python ATM function implementation code example
Python implements 126 mailboxes to send mail
Using Python to implement multiple clipboards
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
Python example to calculate IV value
How to install Python 3.8 on Ubuntu 18.04
How to write classes in python
How to filter numbers in python
Centos6.7 comes with python upgrade to
How to install Python on CentOS 8