【发布时间】:2016-12-27 13:40:44
【问题描述】:
我刚刚在 YouTube 上观看了一个视频,视频中他们讨论了大富翁游戏的数学,除此之外,他们还在下载框中添加了 Python 代码,因此我下载了它来尝试...
这是代码:
import random
from random import shuffle
def monop(finish_order=6,games_order=3):
finish = 10**finish_order
games = 10**games_order
squares = []
while len(squares) < 40:
squares.append(0)
# roll values are values from a six by six grid for all dice rolls
rollvalues = [2,3,4,5,6,7,3,4,5,6,7,8,4,5,6,7,8,9,5,6,7,8,9,10,6,7,8,9,10,11,7,8,9,10,11,12]
games_finished = 0
while games_finished < games:
master_chest = [0,40,40,40,40,10,40,40,40,40,40,40,40,40,40,40]
chest = [i for i in master_chest]
shuffle(chest)
master_chance = [0,24,11,'U','R',40,40,'B',10,40,40,5,39,40,40,40]
chance = [i for i in master_chance]
shuffle(chance)
doubles = 0
position = 0
gos = 0
while gos < finish:
diceroll = int(36*random.random())
if diceroll in [0,7,14,21,28,35]: # these are the dice index values for double rolls
doubles += 1
else:
doubles = 0
if doubles >= 3:
position = 10
else:
position = (position + rollvalues[diceroll])%40
if position in [7,22,33]: # Chance
chance_card = chance.pop(0)
if len(chance) == 0:
chance = [i for i in master_chance]
shuffle(chance)
if chance_card != 40:
if isinstance(chance_card,int):
position = chance_card
elif chance_card == 'U':
while position not in [12,28]:
position = (position + 1)%40
elif chance_card == 'R':
while position not in [5,15,25,35]:
position = (position + 1)%40
elif chance_card == 'B':
position = position - 3
elif position in [2,17]: # Community Chest
chest_card = chest.pop(0)
if len(chest) == 0:
chest = [i for i in master_chest]
shuffle(chest)
if chest_card != 40:
position = chest_card
if position == 30: # Go to jail
position = 10
squares.insert(position,(squares.pop(position)+1))
gos += 1
games_finished += 1
return squares
调用:monopoly-v1.py
现在,当我尝试编译并将其运行到终端时,我得到了“问题”。
通过写作
python monopoly-v1.py
在终端中,我没有收到任何错误或警告,但它没有发生任何事情......
如果我尝试
python monopoly-v1.py
然后
./monopoly-v1.py
那么它是这样说的:
./monopoly-v1.py:第 1 行:意外标记附近的语法错误 ('
./monopoly-v1.py: line 1:def monop(finish_order=6,games_order=3):'
我不明白出了什么问题。对了,python或者python3都是一样的,我的意思是:第一步没有出现错误。
有什么想法吗?
谢谢!
【问题讨论】:
-
您提供的代码仅包含一个 Python 函数。可以这么说,没有“让它去”的代码。
标签: python macos python-2.7 python-3.x compiler-errors