【问题标题】:Syntax Error in Python compiling (Mac Os - Terminal)Python 编译中的语法错误(Mac Os - 终端)
【发布时间】: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


【解决方案1】:

这段代码只是一个函数定义和一些导入。如果你不运行那个函数,它什么也做不了。这就是python script.py 不显示任何内容的原因。

现在,当您尝试这样做时:

./script.py

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


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

【讨论】:

    【解决方案2】:

    只需添加:

    if __name__ == '__main__':
        monop()
    

    在 monopoly-v1.py 的末尾

    【讨论】:

    • 我试图这样做,但是当我编译时它需要很长时间(没有响应..)
    • @ForceBru 我不认为你的评论对我有什么帮助。
    【解决方案3】:

    你没有调用任何你想执行的函数。如果您想从命令行调用monop 函数,您可以使用-c 参数来实现:

    $ python -c 'from monopoly-v1 import monop; print monop(6, 3)'
    

    请注意,如果使用 Python 3,打印函数的语法会有所不同:

    $ python -c 'from monopoly-v1 import monop; print(monop(6, 3))'
    

    【讨论】:

    • 你的语法错误是什么?这里的命令行语法是正确的。
    • 文件 "", 第 1 行 from monopoly-v1.py import monop; print monop(6, 3) ^ SyntaxError: invalid syntax
    • 你在用python 3吗?
    • 两个都可以用,如果你问我的电脑支持什么版本
    • 我在问您使用哪个版本来尝试运行此代码,很清楚; >> print(foo) 是 python 3 语法,>>print foo 仅适用于 python 2
    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2011-09-02
    • 2016-02-08
    • 2012-12-01
    相关资源
    最近更新 更多