日常练手小游戏,60行代码实现的剪刀石头布,别说,你还真不一定能赢过电脑。不信可以试试
# -*- coding:utf8 -*-
import random
def game():
player_score = 0
print('石头剪刀布游戏,输入1代表石头,2代表剪刀,3代表布')
while True:
player = input('你出什么')
if player == '1':
print('你出了石头')
elif player == '2':
print('你出了剪刀')
elif player == '3':
print('你出了布')
else:
print('输入错误,请重新输入!')
continue
player = int(player)
computer = random.randint(1, 3)
if computer == 1:
print('电脑出了石头')
if computer == 2:
print('电脑出了剪刀')
if computer == 3:
print('电脑出了布')
player_score = win(player, computer, player_score)
if player_score == 3:
print('本局结束,玩家获胜!')
break
if player_score == -3:
print('本局结束,电脑获胜!')
break
def win(player, computer, player_score):
if player == computer:
print('平局,不得分,当前玩家分数为{}'.format(player_score))
elif player == 1:
if computer == 2:
player_score += 1
print('玩家赢!当前玩家分数为{}'.format(player_score))
if computer == 3:
player_score -= 1
print('玩家败!当前玩家分数为{}'.format(player_score))
elif player == 2:
if computer == 1:
player_score -= 1
print('玩家败,当前玩家分数为{}'.format(player_score))
if computer == 3:
player_score += 1
print('玩家赢!当前玩家分数为{}'.format(player_score))
elif player == 3:
if computer == 1:
player_score += 1
print('玩家赢!当前玩家分数为{}'.format(player_score))
if computer == 2:
player_score -= 1
print('玩家败,当前玩家分数为{}'.format(player_score))
return player_score
if __name__ == '__main__':
game()