石头剪刀布-python简单实现

import random
print()
print(" *******欢迎来到猜拳游戏********")
print(" *******让我们一局定输赢********")
print(" ***********游戏规则************")
print(" *******0-剪刀 1-石头 2-布******")
print(" ***********开始PK吧************\n")

count1,count2,l=0,0,0
f=0
while l<5:
    y=int(input(" 请输入你的数字:"))
    if y>2 or y<0:
        print(" 输入错误,游戏结束!\n")
        break
    r=random.randrange(0,3)
    print(" 玩家:",y,"  电脑:",r)
    c=y-r
    
    
    if c==-2 or c==1 :
        count1+=1
        print(" 此局玩家胜!\n")
    elif c==0:
        print(' 此局为平局!\n')
    else:
        count2+=1
        print(" 此局电脑胜!\n")
    if count1==3 and l<=5:
        print('\n 玩家先胜三局,玩家胜出!')
        f=1
        break;
        
    if count2==3 and l<=5:
        print(' \n 电脑先胜三局,电脑胜出!')
        f=1
        break
        
    l+=1
        
if f==0:
    print('\n 五局结束,',end='')
    print('玩家胜了',count1,'局,','电脑胜了',count2,'局','\n 最终结果为:',end='')
    if count1>count2:
        print(' 玩家胜!')
    elif count1==count2:
        print(' 平局!')
    else:
      print(' 电脑胜!')

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单Python 3代码,实现了三个人玩石头剪刀布游戏: ```python import random # 定义游戏规则 rules = {'rock': 'scissors', 'scissors': 'paper', 'paper': 'rock'} # 定义玩家列表 players = ['Player 1', 'Player 2', 'Player 3'] # 定义游戏次数 rounds = 3 # 开始游戏 for i in range(rounds): print('Round', i+1) for player in players: # 玩家输入选择 choice = input(player + ', please choose rock, scissors or paper: ') # 电脑随机选择 computer_choice = random.choice(['rock', 'scissors', 'paper']) # 判断胜负 if choice == computer_choice: print(player, 'chose', choice, 'and computer chose', computer_choice, ', it is a tie!') elif rules[choice] == computer_choice: print(player, 'chose', choice, 'and computer chose', computer_choice, ',', player, 'wins!') else: print(player, 'chose', choice, 'and computer chose', computer_choice, ', computer wins!') ``` 代码解释: 1. `rules`定义了游戏规则,即`'rock'`胜过`'scissors'`,`'scissors'`胜过`'paper'`,`'paper'`胜过`'rock'`。 2. `players`定义了玩家列表,本例中有三个玩家。 3. `rounds`定义了游戏次数,本例中为三次。 4. 在游戏开始前,使用`for`循环遍历每一轮游戏,使用`for`循环遍历每一个玩家。 5. 玩家使用`input`函数输入自己的选择,电脑使用`random.choice`函数随机选择。 6. 判断胜负,如果玩家和电脑选择相同,则平局;如果玩家的选择胜过电脑的选择,则玩家获胜;否则,电脑获胜。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值