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

时间:2017-07-02 00:43:55

标签: python-3.x

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

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

移动空间部分应该从最后一部分添加

2 个答案:

答案 0 :(得分:0)

global与var player1一起使用,并每次更改。

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)

参考:
The global statement
Python Scopes and Namespaces

修改
 更进一步,您可以定义一个类:

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()

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

答案 1 :(得分:0)

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

  1. 更改全局变量,然后在其他地方读取全局变量。 @ yi_xiao的答案显示了这个例子。

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

  3. 通常,您应该总是更喜欢返回值而不是改变(更改)全局变量。使用全局变量会使测试变得更难,如果您需要一次从多个线程访问数据,则会成为问题。

    要修复您的代码,您应该从total_dice返回dice_roll,然后传递旧的总数:

    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以外的其他内容。