题目1 :
描述:通过趣味的打怪来学习random随机函数.
代码:
1 from random import *
2 import types
3
4 choc=0
5 hs=[100]
6 numer=[randint(0,100)]
7 tim=[0]
8
9 def story():
10 print('走啊走',end='')
11 a=input()
12 choc=randint(0,1)
13 story() if choc == 0 else num()
14
15 def main():
16 print('游戏规则:你在探险的过程中会遇到怪物,必须靠猜数字才能攻击到它的弱点')
17 print('你可以通过提示攻击偏上说明猜的太大了,偏下说明猜的太小了')
18 story()
19
20 def num():
21 print('你遇到了怪物')
22 while 1:
23 try:
24 putnum=eval(input("请输入您猜测的数字:"))
25 if type(putnum) == type(1):
26 tim[0]+=1
27 if putnum > numer[0]:
28 print("没有攻击到要害,偏上了")
29 if hp() == 0:
30 restart()
31 break
32
33 elif putnum <numer[0]:
34 print("没有攻击到要害,偏下了")
35 if hp() == 0:
36 restart()
37 break
38 elif putnum==numer[0]:
39 print("恭喜您,只用了{}招就打败了怪物".format(tim[0]))
40 break
41 else:
42 print("输入内容必须为整数!")
43 except:
44 print("输入有误!")
45
46
47 def hp():
48 minuhp=randint(1,4)*5
49 hs[0] -= minuhp
50 if hs[0] <=0:
51 hs[0] = 0
52 print('你被怪物打败了.')
53 return 0
54 else:
55 print("您当前的hp值为:",hs[0])
56
57 def restart():
58 chocn=input('是否继续您的探险,[Y/N]? :')
59 if chocn[0] in ['Y','y']:
60 hs[0]=100
61 numer[0]=randint(0,100)
62 tim[0]=0
63 story()
64 else:
65 print('谢谢您的参加!')
66
67 main()
68
运行截图:
题目2:
描述:用turtle库和循环画一条灰色渐变的蛇
代码:
from turtle import *
setup
colormode(255)
pensize(20)
pencolor(255,255,255)
speed(1000)
def changedraw():
penr=254
for i in range(100):
circle(100,1)
penr-=1
pencolor((penr,penr-1,penr-2))
for u in range(100):
circle(-100,1)
penr-=1
pencolor((penr,penr-1,penr-2))
changedraw()
运行截图: