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

如何实现支持字符串输入的石头剪刀布游戏并返回正确结果?

修正文字版石头剪刀布游戏的结果判定问题

我正处于编程学习阶段,尝试开发一款文字版石头剪刀布游戏,希望用户可输入'rock'、'paper'、'scissors'而非0、1、2这类整数。但当前无法正确返回对战结果,尤其是平局判定,原因是字符串与整数无法直接比较。以下是我的代码,请问该如何修正?

import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

computer = [scissors, rock, paper]

print("Welcome to Rock, Paper, Scissors!")
user_input = input("Will you choose Scissors, Paper, or Rock?\n").lower()

if user_input == "rock":
  print(rock)
elif user_input == "paper":
  print(paper)
elif user_input == "scissors":
  print(scissors)

computer_choices = random.randint(0, 2)
print("Computer chose: ")
print(computer[computer_choices])

if user_input == computer_choices:
    print ("It's a draw!")
elif user_input == "rock":
  if computer_choices == 2:
    print ("You lose!")
  else: 
    print("You win!")
elif user_input == "paper":
  if computer_choices == 0:
    print("You lose!")
  else: 
    print("You win!")
elif user_input == "scissors":
  if computer_choices == 1:
    print("You lose!")
  else: 
    print("You win!")

修正方案

核心问题是用户输入的字符串和电脑生成的整数无法直接比较,解决思路是把两者转换成同一类型再进行判断,这里推荐将用户输入映射为对应整数,和电脑选择的整数统一类型:

  1. 创建输入映射字典:根据原代码中computer列表的索引对应关系(0=剪刀,1=石头,2=布),建立用户输入到整数的映射。
  2. 转换用户输入为整数:通过映射字典把用户输入的字符串转成对应整数,同时增加输入有效性验证。
  3. 修正平局与胜负判断逻辑:使用同类型的整数进行比较,确保逻辑正确。

修正后的完整代码

import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

# 电脑选择对应的图案列表,索引0=剪刀,1=石头,2=布
computer_choices_map = [scissors, rock, paper]
# 用户输入到整数的映射,和电脑索引对应
user_choice_map = {'rock': 1, 'paper': 2, 'scissors': 0}

print("Welcome to Rock, Paper, Scissors!")
user_input = input("Will you choose Scissors, Paper, or Rock?\n").lower()

# 验证输入有效性并转换为整数
if user_input not in user_choice_map:
    print("Invalid input! Please enter rock, paper, or scissors.")
else:
    user_choice = user_choice_map[user_input]
    # 打印用户选择的图案
    print(computer_choices_map[user_choice])
    
    # 生成电脑选择的索引
    computer_choice_idx = random.randint(0, 2)
    print("Computer chose: ")
    print(computer_choices_map[computer_choice_idx])
    
    # 判定结果
    if user_choice == computer_choice_idx:
        print("It's a draw!")
    # 石头赢剪刀(1赢0),输布(1输2)
    elif user_choice == 1:
        print("You win!" if computer_choice_idx == 0 else "You lose!")
    # 布赢石头(2赢1),输剪刀(2输0)
    elif user_choice == 2:
        print("You win!" if computer_choice_idx == 1 else "You lose!")
    # 剪刀赢布(0赢2),输石头(0输1)
    elif user_choice == 0:
        print("You win!" if computer_choice_idx == 2 else "You lose!")

关键修改点说明

  • 输入映射与验证:新增user_choice_map字典实现字符串到整数的转换,同时增加输入检查,避免无效输入导致错误。
  • 统一类型比较:将用户输入转为整数后,和电脑生成的索引整数直接比较,解决了原代码中字符串与整数无法比较的问题,平局判定逻辑正常工作。
  • 优化胜负逻辑:基于整数索引的胜负关系更清晰,避免原代码中分支判断的混乱。

内容的提问来源于stack exchange,提问作者Saladin12

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 20:05:06