四十八、创建模块
(六)在apps.py文 件,创建“人机大战小游戏”功能
1 import random #导入random 模块
2 import ybc_ui #导入ybc_ui 模块
3 import ybc_ai #导入 ybc_ai 模块
4
5
6 #创建 rj()功能
7 def rj():
8 #使用 randint()功能
9 # 随机生成一个整数,保存在变量 n1 中
10 n1 = random.randint(1,100)
11 #使用 input number()功能
12 # 输入一个数字,保存在变量 n2 中
13 n2 =ybc_ui.input number(
14 '请输入 1~100 之间的整数')
15 if n2 > n1:#如果 n2 大于n1
16 # 让计算机说出结果
17 ybc ai.speak('数字猜大了,应该小于
18 + str(n2))
19 elif n2< n1:#否则如果 n2 小于 n1
20 # 让计算机说出结果
21 ybc ai.speak('数字猜小了,,应该大于
22 + str(n2))
23 elif n2 == n1:#否则如果 n2 等于 n1
24 #让计算机说出结果
25 ybc ai.speak('数字猜对了,你真棒')
网友评论