我正在尝试开发这个游戏的代码。
在决定了多少轮(3轮、5轮或7轮)之后,我要求输入用户要做的移动。在与艾的选择比较后,我希望每一轮都能显示分数。
我想要显示分数的方式是列表类型: score = ai_score,player_score。但是,每当我试图增加球员的分数时,当我调用print (得分)时,列表就不会用值更新。每一轮打印返回0,0。
我的问题是:你能把变量放在一个列表中然后增加这些变量吗?
谢谢。
请参阅代码:
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')
发布于 2021-04-16 15:06:26
不是,更新变量不会更新列表的值。要使列表中的值保持更新,可以在循环中声明列表。
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')
输出:
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_score
和player_score
为ai_score, player_score = 0, 0
分配了初始分数为零。然后,我们使用score
将这些变量的值赋给score = [ai_score, player_score]
列表。稍后,如果我们更新ai_score
和player_score
的值,则不会更新score
列表。我们需要用新的ai_score
和player_score
值更新ai_score
列表。
发布于 2021-04-16 15:09:03
原始代码:
score = [ai_score, player_score]
为了更新列表中的任何单个项,您可以在其旁边添加方括号,并在其中添加项目索引,然后将其设置为等于要将其更改为的任何值。
在您的例子中,要将ai分数更改为5:
score[0] = 5
其中0是索引(索引开始为0,因此玩家得分指数为1)
https://stackoverflow.com/questions/67127651
复制相似问题