首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python纸剪刀项目-如何在列表中增加变量?

Python纸剪刀项目-如何在列表中增加变量?
EN

Stack Overflow用户
提问于 2021-04-16 15:02:18
回答 2查看 138关注 0票数 1

我正在尝试开发这个游戏的代码。

在决定了多少轮(3轮、5轮或7轮)之后,我要求输入用户要做的移动。在与艾的选择比较后,我希望每一轮都能显示分数。

我想要显示分数的方式是列表类型: score = ai_score,player_score。但是,每当我试图增加球员的分数时,当我调用print (得分)时,列表就不会用值更新。每一轮打印返回0,0。

我的问题是:你能把变量放在一个列表中然后增加这些变量吗?

谢谢。

请参阅代码:

代码语言:javascript
运行
复制
import random

ai_score, player_score=0,0
score = [ai_score, player_score]
game = ['R','P','S']
rounds = int(input('Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?'))

for round in range(rounds):
    player_choice = input('Please choose your move : R for rock, P for paper, S for scissors :')
    ai_choice = random.choice(game)
    if player_choice in game:
        if player_choice == ai_choice:
            print("it's a draw")
        elif player_choice == 'R':
            if ai_choice == 'S':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'P':
                ai_score += 1
                print("You loose this round")
        elif player_choice == 'P':
            if ai_choice == 'R':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'S':
                ai_score += 1
                print("You loose this round")
        elif player_choice == 'S':
            if ai_choice == 'P':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'R':
                ai_score += 1
                print("You loose this round")
        print("This is round %d" % round)
        print("The score is %s" % score)
        print(score)
    else:
        print('Please enter one of the 3 choices')
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-16 15:06:26

不是,更新变量不会更新列表的值。要使列表中的值保持更新,可以在循环中声明列表。

代码语言:javascript
运行
复制
import random

ai_score, player_score = 0, 0
game = ['R','P','S']
rounds = int(input('Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?'))

for round in range(rounds):
    player_choice = input('Please choose your move : R for rock, P for paper, S for scissors :')
    ai_choice = random.choice(game)
    if player_choice in game:
        if player_choice == ai_choice:
            print("it's a draw")
        elif player_choice == 'R':
            if ai_choice == 'S':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'P':
                ai_score += 1
                print("You loose this round")
        elif player_choice == 'P':
            if ai_choice == 'R':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'S':
                ai_score += 1
                print("You loose this round")
        elif player_choice == 'S':
            if ai_choice == 'P':
                player_score += 1
                print('You win the round!')
            elif ai_choice == 'R':
                ai_score += 1
                print("You loose this round")
        print("This is round %d" % round)
        score = [ai_score, player_score]
        print("The score is %s" % score)
        print(score)
    else:
        print('Please enter one of the 3 choices')

输出:

代码语言:javascript
运行
复制
Welcome to the Rock Paper Scissor game. Before starting, how many rounds would you like to choose : 3, 5 or 7?3
Please choose your move : R for rock, P for paper, S for scissors :R
You win the round!
This is round 0
The score is [0, 1]
[0, 1]
Please choose your move : R for rock, P for paper, S for scissors :P
You loose this round
This is round 1
The score is [1, 1]
[1, 1]
Please choose your move : R for rock, P for paper, S for scissors :S
You loose this round
This is round 2
The score is [2, 1]
[2, 1]

解释:

当我们创建一个带有变量的列表时,列表会将这些变量的值放在一起,而不是内存地址。因此,更新变量(稍后)不会影响列表。

在上面的场景中,我们使用ai_scoreplayer_scoreai_score, player_score = 0, 0分配了初始分数为零。然后,我们使用score将这些变量的值赋给score = [ai_score, player_score]列表。稍后,如果我们更新ai_scoreplayer_score的值,则不会更新score列表。我们需要用新的ai_scoreplayer_score值更新ai_score列表。

票数 0
EN

Stack Overflow用户

发布于 2021-04-16 15:09:03

原始代码:

代码语言:javascript
运行
复制
score = [ai_score, player_score]

为了更新列表中的任何单个项,您可以在其旁边添加方括号,并在其中添加项目索引,然后将其设置为等于要将其更改为的任何值。

在您的例子中,要将ai分数更改为5:

代码语言:javascript
运行
复制
score[0] = 5

其中0是索引(索引开始为0,因此玩家得分指数为1)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67127651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档