Python 3.x 我不知道';我不知道为什么变量不';不算
这是到目前为止我的代码,我不知道为什么循环代码时变量不加起来Python 3.x 我不知道';我不知道为什么变量不';不算,python-3.x,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",dic
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
移动空间部分应在上一部分的基础上添加,并在var
player1
中使用global
,并且每次都进行更改
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)
参考资料:编辑:
更进一步,您可以定义一个类:
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()
当有更多玩家时,使用类将使事情变得更容易。问题是,
dice1
、dice2
和total_dice
在函数返回时被“销毁”。这些变量是在函数范围内定义的,因此不能在函数之外使用。有两种方法可以访问函数中的变量:
dice\u roll
返回total\u 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
所有dice\u roll()
变量(dice1
、dice2
和total\u dice
)在函数返回并清除其堆栈后,本地和对它们的任何更改都将消失。我应该如何更改that@samnorman将变量作为参数传入,以便知道前面的值,并在函数结束时返回它们,以便循环稍后返回。@Carcigenicate你能告诉我你的意思吗?我不明白。@samnorman抱歉,这是漫长的一天。我在这里睡一会儿。当我明天起床的时候,如果我有时间,我会写一个例子。尽管在大多数情况下,这应该避免。全球化通常会让你的生活更艰难。@Carcigenicate,是的,你是对的。但是使用globals很少改变代码,而且这个代码似乎是一种实践。我编辑我的答案,使用类而不是全局变量。