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