23-石头剪刀布

  • 阅读: 455
  • 更新: 2022-06-11

难度:

1. 题目描述

2. 关键点

  • if 判断

3. 代码实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import random

lst = ['石头', '剪刀', '布', '石头']

you = input('请出 [石头/剪刀/布]:')
computer = lst[random.randint(0, 2)]

print(f'你出了{you},电脑出了{computer};', end='')
if you == computer:
    print('你俩平手')
elif lst[lst.index(you) + 1] == computer:
    print('你赢了')
else:
    print('你输了')

4. 运行示例

1
2
请出 [石头/剪刀/布]:石头
你出了石头,电脑出了剪刀;你赢了

5. 进阶思考

  1. 思考代码中 lst 为何要多加一个石头

=== 全文完 ===


欢迎加入QQ群:855013471

京公网安备 11011302003970号 京ICP备2022012301号-1