用python写一个石头剪刀布小游戏
时间: 2023-03-15 12:46:18 浏览: 143
好的,我可以给你写一个石头剪刀布小游戏。首先,你需要定义三个变量,分别是石头、剪刀和布:rock = '石头',scissors = '剪刀',paper = '布'。然后,你需要定义一个函数来比较两个变量,也就是玩家和电脑的出拳:def compare(player, computer):
if player == computer:
print('平局')
elif player == rock:
if computer == scissors:
print('玩家赢')
else:
print('电脑赢')
elif player == scissors:
if computer == paper:
print('玩家赢')
else:
print('电脑赢')
elif player == paper:
if computer == rock:
print('玩家赢')
else:
print('电脑赢')最后,你可以定义一个主程序,用于调用上面定义的函数:import randomwhile True:
player = input('请出拳(石头/剪刀/布): ')
computer = random.choice(['石头', '剪刀', '布'])
print('电脑出了:', computer)
compare(player, computer)
answer = input('要继续吗?(y/n)')
if answer == 'y':
continue
else:
print('游戏结束!')
break
阅读全文