Python小游戏:剪刀石头布

日常练手小游戏,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()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值