Python implements simple investment compounding functions and the realization of a roll dice guessing size function

Source: Internet
Author: User

Compound Interest Function:
1 #!/user/bin/env python2 #-*-coding:utf-8-*-3 #Author:qinjiaxi4 defInvest (amount, rate, time):5     Print('princical amount: {}'. Format (amount))6      forTinchRange (1, time + 1):7Amount = Amount * (Rate + 1)8         Print('Year {}: {}'. Format (t, amount))9Invest (2000, 0.5, 5)
Roll dice guess size (three sieves at a time)

Idea: First define a roll dice function, get three sieve random results and put into a list (this process need to import the random function); then define a size-sensitive function that specifies what kind of result returns large, when it returns small, and finally defines a game startup function, A list of default size results is given first, then the user's guess (input) is compared, and some actions such as judgment are used. Source:

1 #!/user/bin/env python2 #-*-coding:utf-8-*-3 #Author:qinjiaxi4 ImportRandom5 #shake three dice at a time and put the results in the list6 defRole_a_dice (number = 3, point =None):7     Print('Let\ ' s play a game')8     ifPoint isNone:9Point = []Ten      whileNumber >0: OnePoint.append (Random.randint (1, 6)) ANumber-= 1 -     return Point - #convert result to ' size ' string the defDice_reslut (total): -IsBig = <= Total <= 18 -Issmall = 3 <= Total <= 10 -     ifIsBig: +         return "Big" -     ifIssmall: +         return  "Small" A defstart_game (): at     Print("-----GAME START-----") -choices = ['Big','Small'] -U_choices = input ('pls enter your choice:') -     ifU_choicesinchChoices: -Points = Role_a_dice ()#Call function Roll dice to get three dice results -Totals = SUM (points)#Three results add final points inResluts = Dice_reslut (totals)#call the function to get the final point converted to a string -         ifU_choices = =resluts: to             Print('The points are: {} Congratulations on your guess.'. Format (points)) +         Else: -             Print('the number of points is: {} sorry, wrong guess.'. Format (points)) the     Else: *         Print('Invalid words.') $ Start_game ()Panax NotoginsengStart_game ()

Python implements simple investment compounding functions and the realization of a roll dice guessing size function

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.