Python to buy beer (2 yuan a bottle, 4 caps for a bottle, 2 bottles for a bottle)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.