m0_62607163 2021-10-06 03:20 采纳率: 77.8%
浏览 88
已结题

用python写石头剪刀布的游戏

def rps_round(comp_move):
x=input('Enter R, P, or S:')
if comp_move=='R':
if x=='R':
print('Computer selects',comp_move)
print('Tie!')
return 'T'
elif x=='P':
print('Computer selects',comp_move)
print('Player wins!')
return 'P'
elif x=='S':
print('Computer selects',comp_move)
print('Computer wins!')
return 'C'
else:
print('Invalid Move')
return rps_round('R')
elif comp_move=='P':
if x=='R':
print('Computer selects',comp_move)
print('Computer wins!')
return 'C'
elif x=='P':
print('Computer selects',comp_move)
print('Tie!')
return 'T'
elif x=='S':
print('Computer selects',comp_move)
print('Player wins!')
return 'P'
else:
print('Invalid Move')
return rps_round('P')
elif comp_move=='S':
if x=='R':
print('Computer selects',comp_move)
print('Player wins!')
return 'P'
elif x=='P':
print('Computer selects',comp_move)
print('Computer wins!')
return 'C'
elif x=='S':
print('Computer selects',comp_move)
print('Tie!')
return 'T'
else:
print('Invalid Move')
return rps_round('S')
这是rps_round的代码

现在编写一个函数rps_game(),它不接受任何参数,并玩石头剪刀布,直到玩家连续赢了3轮。打成平局算作一轮,应该将你的“连胜”计数器重置为0,但无效移动不算作一轮。你的功能还应该记录玩家完成这项任务所需的局数。您必须调用问题C中的rps_round函数才能执行此操作。
计算机应按如下方式移动:
如果是第一轮,电脑就出石头R。
如果是第二轮,电脑就出布P。
如果是第三轮,电脑就出剪刀S。
重复这个模式,在第四轮中再次使用石头。
每次调用rps_round时,都需要将其作为参数传入
(这并不是一个特别好的石头剪纸策略,但目标是测量用户花了多长时间来选择图案)。
打印出每轮比赛的回合数和玩家在每轮比赛后连续获胜的次数。一旦用户对计算机连续赢了三轮,该函数应返回所玩的总回合数。
提示:您将需要变量来跟踪轮数和玩家连续赢了多少轮。
约束:必须从rps_游戏中调用rps_round函数才能玩每一轮:不要重做rps_游戏中问题C的任何工作。
例子如下

rps_game()
Enter R, P, or S: P
Computer selects R
Player wins!
Rounds: 1 - Consecutive Wins: 1
Enter R, P, or S: S
Computer selects P
Player wins!
Rounds: 2 - Consecutive Wins: 2
Enter R, P, or S: R
Computer selects S
Player wins!
Rounds: 3 - Consecutive Wins: 3
3

rps_game()
Enter R, P, or S: P
Computer selects R
Player wins!
Rounds: 1 - Consecutive Wins: 1
Enter R, P, or S: S
Computer selects P
Player wins!
Rounds: 2 - Consecutive Wins: 2
Enter R, P, or S: S
Computer selects S
Tie!
Rounds: 3 - Consecutive Wins: 0
Enter R, P, or S: S
Computer selects R
Computer wins!
Rounds: 4 - Consecutive Wins: 0
Enter R, P, or S: Paper
Invalid Move
Enter R, P, or S: B
Invalid Move
Enter R, P, or S: S
Computer selects P
Player wins!
Rounds: 5 - Consecutive Wins: 1
Enter R, P, or S: R
Computer selects S
Player wins!
Rounds: 6 - Consecutive Wins: 2
Enter R, P, or S: P
Computer selects R
Player wins!
Rounds: 7 - Consecutive Wins: 3
7

rps_game()
Enter R, P, or S: R
Computer selects R
Tie!
Rounds: 1 - Consecutive Wins: 0
Enter R, P, or S: R
Computer selects P
Computer wins!
Rounds: 2 - Consecutive Wins: 0
Enter R, P, or S: R
Computer selects S
Player wins!
Rounds: 3 - Consecutive Wins: 1
Enter R, P, or S: Rock
Invalid Move
Enter R, P, or S: P
Computer selects R
Player wins!
Rounds: 4 - Consecutive Wins: 2
Enter R, P, or S: These
Invalid Move
Enter R, P, or S: Don't
Invalid Move
Enter R, P, or S: Count
Invalid Move
Enter R, P, or S: S
Computer selects P
Player wins!
Rounds: 5 - Consecutive Wins: 3
5

  • 写回答

2条回答 默认 最新

  • 关注

    你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)

    def rps_round(comp_move):
        x=input('Enter R, P, or S:')
        if comp_move=='R':
            if x=='R':
                print('Computer selects',comp_move)
                print('Tie!')
                return 'T'
            elif x=='P':
                print('Computer selects',comp_move)
                print('Player wins!')
                return 'P'
            elif x=='S':
                print('Computer selects',comp_move)
                print('Computer wins!')
                return 'C'
            else:
                print('Invalid Move')
                return rps_round('R')
        elif comp_move=='P':
            if x=='R':
                print('Computer selects',comp_move)
                print('Computer wins!')
                return 'C'
            elif x=='P':
                print('Computer selects',comp_move)
                print('Tie!')
                return 'T'
            elif x=='S':
                print('Computer selects',comp_move)
                print('Player wins!')
                return 'P'
            else:
                print('Invalid Move')
                return rps_round('P')
        elif comp_move=='S':
            if x=='R':
                print('Computer selects',comp_move)
                print('Player wins!')
                return 'P'
            elif x=='P':
                print('Computer selects',comp_move)
                print('Computer wins!')
                return 'C'
            elif x=='S':
                print('Computer selects',comp_move)
                print('Tie!')
                return 'T'
            else:
                print('Invalid Move')
                return rps_round('S')
    
    def rps_game():
        rps = ["R","P","S"]
        rounds = 0
        wins = 0
        while wins < 3:
            d = rps_round(rps[rounds%3])
            rounds += 1
            if d=="P":
                wins += 1
            else:
                wins = 0
            print(f"Rounds: {rounds} - Consecutive Wins: {wins}")
        return rounds
    
    print(rps_game())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月14日
  • 已采纳回答 10月6日
  • 修改了问题 10月6日
  • 创建了问题 10月6日

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?