所以我决定用Python编写Monopoly,但是我在更新玩家位置时遇到了一些麻烦.我写了一个for循环,遍历玩家,为每个玩家掷骰子,然后更新他们的位置.问题是位置变量没有保留最新位置,它在for循环开始时保持重置为0.这是我的代码:
player1location = 0
def turn(numberPlayers, player, player1location, Board):
for player in range(numberPlayers):
player = 'Player'+str(player+1)
print 'It\'s', player, 'turn!'
print player1location
rollDice = raw_input('Press Enter to roll the dice!')
diceRoll = random.randint(1,6)
print player, 'rolled a', diceRoll
player1location = player1location + diceRoll
print 'You landed on', player1location
print '\n'
while True:
turn(numberPlayers, player, player1location, Board)
Run Code Online (Sandbox Code Playgroud)
如有必要,我可以提供更多代码,但我认为这是控制玩家位置的所有内容.谢谢!
编辑:显然我正在改变局部变量而不是全局变量.我将如何更改全局变量?
我已经开始自学python,并且注意到与全局变量和范围有关的某些奇怪事情。当我运行这个:
x = 2
y = 3
z=17
def add_nums():
y = 6
return z+y
Run Code Online (Sandbox Code Playgroud)
打印结果为23 ...但是,当我将返回值扩展为:
x = 2
y = 3
z=17
def add_nums():
y = 6
z = z + y
return z
Run Code Online (Sandbox Code Playgroud)
我在第6行收到以下错误:
Local name referenced but not bound to a value.
A local name was used before it was created. You need to define the
method or variable before you try to use it.
Run Code Online (Sandbox Code Playgroud)
我很困惑为什么我在这里遇到错误,因为z是全局可访问的。
下面是我的代码
global PostgresDatabaseNameSchema
global RedShiftSchemaName
PostgresDatabaseNameSchema = None
RedShiftSchemaName = None
def check_assign_global_values():
if not PostgresDatabaseNameSchema:
PostgresDatabaseNameSchema = "Superman"
if not RedShiftSchemaName:
RedShiftSchemaName = "Ironman"
check_assign_global_values()
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误说
Traceback (most recent call last):
File "example.py", line 13, in <module>
check_assign_global_values()
File "example.py", line 8, in check_assign_global_values
if not PostgresDatabaseNameSchema:
UnboundLocalError: local variable 'PostgresDatabaseNameSchema' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
那么我们不能从函数内部访问或设置全局变量吗?
我在python中使用全局变量时遇到麻烦......
在我的程序中,我声明了2个全局变量,全局SYNC_DATA和全局SYNC_TOTAL_SIZE
现在在我的一个函数中,我能够使用全局变量SYNC_DATA而不在函数中再次将其声明为全局变量; 但是,我无法以相同的方式使用其他全局变量SYNC_TOTAL_SIZE.我必须再次声明后者在函数中是全局的才能使用它.我得到这个错误,如果我使用它而不在函数中声明为全局 - "UnboundLocalError:局部变量'SYNC_TOTAL_SIZE'在赋值之前被引用"
为什么有时我可以访问全局变量而不将它们声明为函数中的全局变量而有时不是?为什么我们必须再次在函数中声明它是全局函数,因为它在开始时已经被声明过一次...为什么函数只检查全局命名空间中的变量,如果它没有在其中找到它命名空间?
我写了一个看起来像这样的测试程序:
#!/usr/bin/python
def incrementc():
c = c + 1
def main():
c = 5
incrementc()
main()
print c
Run Code Online (Sandbox Code Playgroud)
我认为,因为我在main的主体内调用了incrementc,所以main中的所有变量都会传递给incrementc.但是当我运行这个程序时,我得到了
Traceback (most recent call last):
File "test.py", line 10, in <module>
main()
File "test.py", line 8, in main
incrementc()
File "test.py", line 4, in incrementc
c = c + 1
UnboundLocalError: local variable 'c' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
为什么c不通过?如果我想让一个变量被多个函数引用,我是否必须全局声明它?我在某处看到全局变量很糟糕.
谢谢!
当调用tst时,为什么下面的变量(A,B,C,D)没有改变.
A,B,C = 0,0,0
D = 0
def tst():
A,B,C = 1,2,3
D = 4
print(A,B,C,D)
tst() # tst is called
print(A,B,C,D)
Output:
(1, 2, 3, 4)
(0, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud) 我一直在尝试在变量函数中返回一个变量,并在它之外使用它:
test = 0
def testing():
test = 1
return test
testing()
print(test)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,结果是0.我怎么能解决这个问题?
我想要完成这个程序需要一些帮助.这件事情有点乱.它结束了12小时的一天,它将在今晚到期,我想我现在只是让事情变得更糟.我正在努力为player_choice实现"4"的输入并让程序只是说'退出程序'并停止.当我输入当前的选择时,我得到了我认为无限循环,因为整个IDE锁定并崩溃.
计划要求:
1) Get and return the value of the computer’s choice as an integer.
2) Use a menu to get, validate, and return the player’s choices.
The player will enter their menu choice for rock, paper, scissors, or quit.
Validate the choice before returning it.
3) Determine the winner. There are a total of 9 possible combinations of
computer and player choices. The menu should always be displayed after the outcome has
been displayed regardless of the outcome
(i.e. …Run Code Online (Sandbox Code Playgroud) # left rotate using slicing
def leftRotate(arr, k, n):
arr = arr[k:] + arr[:k]
print(arr)
arr = [1, 2, 3, 4, 5, 6, 7]
leftRotate(arr, 2, 7)
print(arr)
Run Code Online (Sandbox Code Playgroud)
结果:
[3, 4, 5, 6, 7, 1, 2]
[1, 2, 3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)
当我在函数外部打印数组时,它不再旋转,并保持原来的状态.有人能帮助我理解这个吗?
我正在使用 discord.py 在 Python 中制作一个 Discord 机器人。我想从异步线程设置/修改全局变量。
message = ""
@bot.command()
async def test(ctx, msg):
message = msg
Run Code Online (Sandbox Code Playgroud)
然而这行不通。我怎样才能做到这一点?
python ×10
function ×3
variables ×2
asynchronous ×1
discord.py ×1
jes ×1
list ×1
python-3.x ×1
return-value ×1
scope ×1