You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python博弈游戏代码使其运行5次并统计最终总分?

游戏循环与总分统计修改方案

作为Python新手,希望修改现有Game Theory游戏代码,让游戏运行5次并在最后计算所有回合的总分。原代码仅运行一次,无累计统计功能,以下是修改后的完整实现:

import random 

user_play = input("Do you want to play a Game Theory Situation (Henry's Version)? ").lower()

if "yes" in user_play:
    name = input("Hi, and welcome to Henry's House! Tell me your name! ").capitalize()
    
    print(f"\n{name}, this is a simple game, you and your nemesis will win money if you beat me")
    
    print("""
    You and your nemesis are in 2 different rooms at Henry's House. 
    You can't communicate with anyone but Henry.""")
    
    print(f"""
    Henry: {name}, this is the case, it's pretty simple. You just have to decide
    if you want to pull the string or not. And here is the outcome.
    \n
    {name} \ Your nemesis\t\t Yes \t\t\t No
    --------------\t --------------------------\t ---------------------------
    Yes\t\t\t Both get $50 each\t\t You get $10, your nemesis $100
    No \t\t\t You get $100, your nemesis $10\t $0 for both
    """)
    
    board_game = [[3, 1], [5, 0]]
    
    dictionary_results = {"(Y, Y)": (board_game[0][0], board_game[0][0]),
                          "(Y, N)": (board_game[0][1], board_game[1][0]),
                          "(N, Y)": (board_game[1][0], board_game[0][1]),
                          "(N, N)": (board_game[1][1], board_game[1][1])}
    
    # 初始化总分变量,用于累计5轮分数
    total_user = 0
    total_nemesis = 0
    
    # 用for循环控制游戏运行5次
    for round_num in range(1, 6):
        print(f"\n=== 第 {round_num} 回合 ===")
        user_choose = input("""
        Do you want to pull the string?
        Select:
        - 'Y' is Yes
        - 'N' is No
        """).upper()
            
        nemesis_choose = random.randint(0,1)
        
        # 初始化单回合结果变量,避免未定义错误
        user_result = 0
        nemesis_result = 0
        
        if user_choose == "Y" and nemesis_choose == 0:
            user_result, nemesis_result = dictionary_results["(Y, Y)"]
            print(f"""
        You do, and so does your nemesis!
        You earned {user_result} points.
        Your nemesis earned {nemesis_result}""")
        
        elif user_choose == "N" and nemesis_choose == 0:
            user_result, nemesis_result = dictionary_results["(N, Y)"]
            print(f"""
        You don't, but your nemesis does :(
        You earned {user_result} points.
        Your nemesis earned {nemesis_result}""")

        elif user_choose == "Y" and nemesis_choose == 1:
            user_result, nemesis_result = dictionary_results["(Y, N)"]
            print(f"""
        You do, but your nemesis doesn't want to :(
        You earned {user_result} points.
        Your nemesis earned {nemesis_result}""")

        elif user_choose == "N" and nemesis_choose == 1:
            user_result, nemesis_result = dictionary_results["(N, N)"]
            print(f"""
        You both chose not to pull!
        You earned {user_result} points.
        Your nemesis earned {nemesis_result}""")
        else:
            print("Invalid selection! Please choose Y or N.")
            continue  # 跳过无效输入的回合,不统计分数
        
        # 累计当前回合分数到总分
        total_user += user_result
        total_nemesis += nemesis_result
        
        # 单回合胜负判断
        if user_result > nemesis_result:
            print(f"Congrats, you won this round!")
        elif user_result == nemesis_result:
            print(f"Well we tie this round!")
        else:
            print(f"Congrats to me, I beat you this round!")
    
    # 输出最终总分和全局胜负结果
    print("\n=== 最终结果 ===")
    print(f"Your total points: {total_user}")
    print(f"Your nemesis's total points: {total_nemesis}")
    
    if total_user > total_nemesis:
        print(f"Congrats {name}, you won the entire game!")
    elif total_user == total_nemesis:
        print(f"Well {name}, we tie the entire game!")
    else:
        print(f"Congrats to me, I beat you {name}!")
else:
    print("Maybe next time!")

关键修改说明

  • 总分初始化:在循环前定义total_user和total_nemesis变量,用于累计5轮的分数。
  • 回合控制:使用for round_num in range(1,6)实现5次游戏循环,同时显示当前回合数。
  • 分数累计:每轮结束后将单回合结果加到总分变量中。
  • 错误处理:添加无效输入判断,跳过无效选择的回合统计,避免程序报错。
  • 结果展示:循环结束后输出总分,并根据总分判断最终胜负。

内容的提问来源于stack exchange,提问作者Henry Huynh Pham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 20:25:56