Weekend to do nothing, see the group interactive answer, just these two days in self-study python, by the way practice grammar.
Title: Beer 2 Yuan a bottle, 4 caps for a bottle, 2 bottles for a bottle, ask 10 yuan can drink a few bottles?
The first thing we need to do to get things into code in life is to find out the law of things:
Drinking bottle caps and bottles, caps and bottles can also be changed, wine and bottle caps and bottles, so constantly circulating, it is easy to think of recursion. As long as the details of a little control, you can complete the calculation.
The code is as follows:
#!/usr/bin/env Python3#-*-coding:utf-8-*-Count =0surplus_bottletop=0surplus_bottle=0defDrinkandsurplus (Bottletop, bottle):GlobalCount,surplus_bottletop,surplus_bottle Beers=0 Beers+ = BOTTLETOP//4Beers+ = BOTTLE//2Count+=Beers Bottletop= bottletop%4 +Beers Bottle= bottle%2 +BeersPrint('This time drank%d bottle, the remaining bottle cap%d, the remaining bottle%d'%(beers, bottletop, bottle))if(Bottletop//4 > 0orBOTTLE//2 >0): Drinkandsurplus (Bottletop, bottle)Else: Surplus_bottletop=Bottletop Surplus_bottle=Bottleif __name__=='__main__': Money= 10count, Surplus_bottletop, Surplus_bottle= MONEY//2, MONEY//2, MONEY//2Print('A total of%d yuan, this time drink%d bottles, the remaining bottle%d, the remaining bottles%d'%(Money,count,count,count)) Drinkandsurplus (Surplus_bottletop, Surplus_bottle)Print('A total of%d bottles, the remaining caps%d, the remaining bottles%d'% (Count,surplus_bottletop,surplus_bottle))
The results of the implementation are as follows:
Python novice, big God do not spray!
Python to buy beer (2 yuan a bottle, 4 caps for a bottle, 2 bottles for a bottle)