发布时间:2019-09-08 09:10:08编辑:auto阅读(1757)
骰子游戏:
#!/usr/bin/env python3.5 //指定python的版本 #File: dice.py import random //导入随机库 for i in range(1,6): //range表示范围,从1开始不包括6,依次执行5次 random1 = random.randint(1,6) random2 = random.randint(1,6) //重复一次,总共做两次这个操作 total = random1 + random2 print (total) //将取得值打印到屏幕 if total > 10: //如果值大于10 print ('GOOD!') elif total < 4: //如果值小于4 print ('BAD!') elif random1 == random2: //如果两次的值相等 print ('DOUBLE!')
注:将骰子掷出5次,每次取得1-6中一个数,称为一个操作,一共做两个操作。
扩展:
使用while语句,当掷出两个6时,退出,否则一直掷
上一篇: python-检测ssh端口
下一篇: Python监控文件内容变化
48820
47877
38655
35824
30244
26994
26037
20865
20658
19027
446°
531°
551°
563°
544°
528°
596°
660°
781°
844°