复利函数:
1 #!/user/bin/env python
2 #-*-coding:utf-8 -*-
3 #Author: qinjiaxi
4 def invest(amount, rate, time):
5 print('princical amount: {}'.format(amount))
6 for t in range(1, time + 1):
7 amount = amount * (rate + 1)
8 print('year {}: {}'.format(t, amount))
9 invest(2000, 0.5, 5)摇骰子猜大小(一次三个筛子)
思路:首先定义一个摇骰子函数,得到三个筛子随机结果并存入一个列表中(这个过程中需要导入random函数);然后定义一个区分大小的函数,规定什么样的结果返回大,什么时候返回小;最后定义一个游戏启动函数,先给出系统默认大小结果的列表,然后将用户的猜测(输入)进行对比,其中用到判断等一些操作。源码:
1 #!/user/bin/env python
2 #-*-coding:utf-8 -*-
3 #Author: qinjiaxi
4 import random
5 #一次摇三个骰子并将结果存在列表中
6 def role_a_dice(number = 3, point = None ):
7 print('Let\'s play a game')
8 if point is None:
9 point = []
10 while number > 0:
11 point.append(random.randint(1, 6))
12 number -= 1
13 return point
14 #将结果转换成'大小'字符串
15 def dice_reslut(total):
16 isBig = 11 <= total <= 18
17 isSmall = 3 <= total <= 10
18 if isBig:
19 return "Big"
20 if isSmall:
21 return "Small"
22 def start_game():
23 print("-----GAME START-----")
24 choices = ['Big', 'Small']
25 U_choices = input('pls enter your choice:')
26 if U_choices in choices:
27 points = role_a_dice()#调用函数摇骰子得到三个骰子的结果
28 totals = sum(points)#三次结果相加得到最终点数
29 resluts = dice_reslut(totals)#调用函数得到将最终点数转换成字符串
30 if U_choices == resluts:
31 print('点数是:{}恭喜你猜对了'.format(points))
32 else:
33 print('点数是:{}抱歉猜错了'.format(points))
34 else:
35 print('Invalid words.')
36 start_game()
37 start_game()
















