我不知道为什么变量不加起来



这是我到目前为止的代码,我不知道为什么当我循环代码时变量没有加起来。

import random
player1=1
player2=1
print("Welcome To My Board Game")
def dice_roll():
dice1=random.randint(1,6)
dice2=random.randint(1,6)
print("Your first roll is",dice1)
print("Your second roll is",dice2)
total_dice=(dice1+dice2)
print("Your total is",total_dice)
print("You moved",total_dice,"spaces, to space",player1+total_dice )

while True:
print("Player 1 Turn")
choice=input("Do you want to roll the dice press r ")
if choice.lower()=="r":
dice_roll()

这是我的代码的输出

Welcome To My Board Game
Player 1 Turn
Do you want to roll the dice press r r
Your first roll is 6
Your second roll is 5
Your total is 11
You moved 11 spaces, to space 12
Player 1 Turn
Do you want to roll the dice press r r
Your first roll is 4
Your second roll is 5
Your total is 9
You moved 9 spaces, to space 10
Player 1 Turn
Do you want to roll the dice press r

移动空间部分应从上一部分添加

问题是,当函数返回时,dice1dice2total_dice被"销毁"。这些变量是在函数范围内定义的,因此不能在函数外部使用。有两种方法可以访问函数内的变量:

  1. 更改全局变量,然后在其他地方读取全局变量。 @yi_xiao的回答就是一个例子。

  2. 传入一些数据
  3. ,并返回一些数据。

通常,您应该始终首选返回值,而不是改变(更改(全局变量。使用 globals 变量会使测试更加困难,如果需要一次从多个线程访问数据,则会成为一个问题。

要修复您的代码,您应该从dice_roll返回total_dice并将旧的总数传入:

import random
player1=1
player2=1
print("Welcome To My Board Game")
# Pass the old total in as total_dice
def dice_roll(total_dice):
dice1=random.randint(1, 6)
dice2=random.randint(1, 6)
print("Your first roll is", dice1)
print("Your second roll is", dice2)
# Increase the running total sum
total_dice += dice1 + dice2 
print("Your total is", total_dice)
print("You moved", total_dice, "spaces, to space", player1 + total_dice)
# Return the sum
return total_dice

# Note, in this simple example, sum is actually global.
# In more realistic cases, this would be inside of a function
#  instead of being global 
sum = 0
while True:
print("Player 1 Turn")
choice = input("Do you want to roll the dice press r ")
if choice.lower() == "r":
# Pass sum to dice_roll
# Reassing sum to the new sum given to us by dice_roll
sum = dice_roll(sum)

另请注意,目前,您的循环永远不会退出。即使有人键入"r"以外的内容,循环也不会退出,您需要告诉它退出。您可以使用break,也可以从无限循环更改为while True以外的其他内容。

global与您的 varplayer1一起使用,并每次更改它。

def dice_roll():
dice1=random.randint(1,6)
dice2=random.randint(1,6)
print("Your first roll is",dice1)
print("Your second roll is",dice2)
total_dice=(dice1+dice2)
print("Your total is",total_dice)
global player1
player1 += total_dice
print("You moved",total_dice,"spaces, to space",player1)

参考:
全局语句
Python 作用域和命名空间

编辑:更进一步,你可以定义一个类:

import random

class player:
def __init__(self, init_space=1):
self.space = init_space
def dice_roll(self):
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print("Your first roll is",dice1)
print("Your second roll is",dice2)
total_dice = (dice1 + dice2)
print("Your total is",total_dice)
self.space += total_dice
print("You moved",total_dice,"spaces, to space", self.space)

player1 = player()
player2 = player()
print("Welcome To My Board Game")
while True:
print("Player 1 Turn")
choice = input("Do you want to roll the dice press r ")
if choice.lower() == "r":
player1.dice_roll()

当玩家更多时,使用类会让事情变得更容易。

最新更新