YouTip LogoYouTip

Python Exercise Example21

# Python2.x Python Exercise Instance 21 [![Image 3: Python 100 Examples](#) Python 100 Examples](#) **Question:** Monkey eating peaches problem: A monkey picked some peaches on the first day, ate half of them immediately, and was still not satisfied, so it ate one more. The next morning, it ate half of the remaining peaches and one more. From then on, every morning it ate half of the remaining peaches from the previous day plus one more. On the morning of the 10th day, when it wanted to eat again, it saw only one peach left. Find out how many peaches were picked on the first day. **Program Analysis:** Adopt a reverse thinking approach, inferring backwards from the end. Program source code: ## Instance(Python 2.0+) #!/usr/bin/python# -*- coding: UTF-8 -*-x2 = 1 for day in range(9,0,-1): x1 = (x2 + 1) * 2 x2 = x1 print(x1) The output of the above instance is:
← Python Exercise Example22Python Exercise Example20 β†’