0

I just watched a video on YouTube in which they discuss about the maths of the Monopoly game, and besides all the things, they added the Python code in the download box, hence I downloaded it to try it...

This is the code:

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

Called: monopoly-v1.py

Now, when I try to compile and run it into the terminal I get "problems".

By writing

python monopoly-v1.py

in the terminal, I don't get any error or warning, but it does not happen anything...

If I try with

python monopoly-v1.py

and then

./monopoly-v1.py

then here is what it says:

./monopoly-v1.py: line 1: syntax error near unexpected token (' ./monopoly-v1.py: line 1:def monop(finish_order=6,games_order=3):'

I don't understand what is wrong. By the way, python or python3 are the same, I mean: no error appears at the first step.

Any idea?

Thank you!

1
  • 1
    The code you've presented consists of just one Python function. There's no code to 'make it go', so to speak. Commented Dec 27, 2016 at 13:46

3 Answers 3

4

This code is merely a function definition and some imports. It will do nothing if you don't run that function. This is why python script.py doesn't show anything.

Now, when you try to do this:

./script.py

The shell tries to execute Python code as if it was written in BASH (or, more generally, as if it was a shell script), which results in an error, of course. Why does it do so? Because it's told to execute via the ./ structure, but cannot find anything to execute it with1. So, it finally tries to run it as a shell script.


1. And the shell actually does the search. For example, if you prefixed your code with a special shebang, it would try to run it as Python code: #!python or #!env python or #!/usr/bin/env python or even #!/path/to/python

Sign up to request clarification or add additional context in comments.

Comments

2

just add:

if __name__ == '__main__':
    monop()

at the end of monopoly-v1.py

2 Comments

I tried to do that, but when I compile it takes forever (no response..)
@ForceBru I don't think your comment helped me somehow.
2

You have not called any function you wish to execute. If you would like to call the monop function from the command line, you might do so by using the -c argument:

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

Note that the print function syntax will be different if using Python 3:

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

5 Comments

What is your syntax error? The command line syntax here is correct.
File "<string>", line 1 from monopoly-v1.py import monop; print monop(6, 3) ^ SyntaxError: invalid syntax
Are you using python 3?
I can use both of them, if you are asking about what version does my computer support
I am asking which version you are using to attempt to run this code, clearly; >> print(foo) is python 3 syntax, >>print foo only works in python 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.