gpt4 book ai didi

python - Python 编译中的语法错误(Mac Os - 终端)

转载 作者:行者123 更新时间:2023-12-02 10:49:09 25 4
gpt4 key购买 nike

我刚刚在 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

称为:垄断-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 script.py不显示任何东西。

现在,当您尝试执行此操作时:

./script.py

shell 尝试执行 Python 代码,就好像它是用 BASH 编写的一样(或者更一般地说,就好像它是一个 shell 脚本),这当然会导致错误。为什么会这样做?因为它被告知通过 ./ 执行结构,但找不到任何东西来执行它1。因此,它最终尝试将其作为 shell 脚本运行。

1. shell 实际进行搜索。例如,如果您在代码前面加上一个特殊的 shebang,它会尝试将其作为 Python 代码运行: #!python#!env python#!/usr/bin/env python甚至 #!/path/to/python

关于python - Python 编译中的语法错误(Mac Os - 终端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41346282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com