猜拳游戏
下面我们利用if判断语句编写一个猜拳游戏的代码。
import random
player = int(input('请输入 (0)石头 (1)剪刀 (2) 布:')) # 此处要转化成int 0 =='0'结果为False
print('用户输入的是',player)
# input是用来接收用户输入的内容
# 电脑应该随机出一个数字[0,2]
# 需要使用随机数模块 random
# random.randint(a,b) 能够生成[a,b]的整数
computer = random.randint(0,2)
print('电脑输入的是:',computer)
if (player == 0 and computer ==1) or (player ==1 and computer == 2) or (player == 2 and computer == 0):
print('恭喜你,你赢了')
elif player == computer:
print('平局')
else:
print('你个垃圾,输了吧!')
和电脑玩猜拳,怎么才能欺负电脑保证每次都赢呢?
我们只需要修改以下代码的顺序,让电脑先出即可!
import random
computer = random.randint(0,2)
print('电脑输入的是:',computer)
player = int(input('请输入 (0)石头 (1)剪刀 (2) 布:')) # 此处要转化成int 0 =='0'结果为False
print('用户输入的是',player)
if (player == 0 and computer ==1) or (player ==1 and computer == 2) or (player == 2 and computer == 0):
print('恭喜你,你赢了')
elif player == computer:
print('平局')
else:
print('你个垃圾,输了吧!')