一个简单的python石头剪刀布小游戏
导入 random包 和 time包
random.choice :从序列中获取一个随机元素
time.sleep :设定循环睡眠时间(秒)
import random
import time
while True:
player = int(input('请输入要出的拳 石头(1)/剪刀(2)/布(3):'))
if player not in [1,2,3]:
print('请输入正确的手势')
continue
computer = random.randint(1,3)
print('玩家出的拳是: %s' % player)
print('电脑出的拳是: %s' % computer)
if ((player == 1 and computer == 2)
or (player == 2 and computer == 3)
or (player == 3 and computer == 1)):
print('玩家胜利')
elif player == computer:
print('平局')
else:
print('电脑胜利')
time.sleep(1)