**Python code to find bugs (5) **
**Code design requirements of the previous period: **
Use the nesting of conditional operators to complete this question: students with academic performance >=90 points are represented by A, those with a score of 60-89 are represented by B, and those with a score of less than 60 are represented by C.
code show as below:
Please read the code carefully to find out the bugs!
**Correct answer: **There are 3 bugs in total. (For the answer time, see how many answers are correct?)
(1) The return value of the input() method is a string, which requires forced type conversion to participate in the calculation. This is not the first time I have encountered it, haha, it depends on if anyone has a memory.
(2) grade = A / B / C, where A, B, and C are all strings and should be enclosed in quotation marks. Otherwise, what they are like now? ... is an undefined variable and cannot be assigned to another variable.
(3) There is another magical error: how does else if look so familiar? That's right, it's written in many languages, here...Is your hand slippery, haha! Python is very simple, just need elif.
So, the correct code should look like this:
Well, this one looks more "pure"!
**Comment: **The main purpose of finding bugs yesterday:
(1) Let everyone consolidate the use of input () return value.
(2) Pay attention to the standard use of character strings.
(3) Pay attention to the standard use of conditional judgment sentences.
For students who have a vague understanding of the above knowledge points, please read and study the "Introduction to Python" sent outside of the Gaodu Express, or the video course "Introduction to Python Easy" on the Gaodu website.
Today’s question is announced below.
**Code design requirements for this issue: **
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?
Demand analysis: This is a common mathematical problem. See how to solve it by programming?
code show as below:
Have you noticed? This code is very interesting!
The loop statement must be used. If you use the cumulative sum method, you can directly obtain the number of meters the ball has passed during the Nth landing. The cumulative half (divided by 2) calculation can also directly obtain the final rebound height.
But, why define two lists?
I recently talked to a friend about Python and discovered this interesting feature. Many friends who write Python code have such a habit. It is natural for everyone to record the behavior of a repetitive sequence in a list of data. Then, you Which item in it is needed, it is convenient for you at any time.
Rather than our usual practice, through loops, the code directly obtains the final desired result after the calculation, and ignores the intermediate state data.
I feel that these are two different thinking habits. I guess, is it related to Python's genes? !
Well, the problem is that this beautiful code is still a bit flawed, where is the **bug? **
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