What simple way can I run python programs?

Asked

Viewed 282 times

0

My question is quite beginner level, excuse me. I use pycharm but I am open to other better suggestions according to my problem. I would like to run the code on the interpreter himself, but according to the book I practice, all the examples as one I will post right away, follow the different methodology than the one I was adapted to, it may be from me: Lack of custom; either the book has this proposal exactly as a challenge; or I am making a mockery of the reading. What bothers me and makes my learning difficult is the code being described in a file and the call of the methods etc... made in another, in the terminal (>>>). So all the while I’m forced to copy the code to the terminal.
- Example taken from the book Fluent Python (Luciano Ramalho).

import collections
from random import choice

Card  = collections.namedtuple('Card',['rank', 'suit'])

class FrenchDeck:
    ranks =  [str(n) for n in range (2, 11)] + list ('JQKA')
    suits = 'spades diamonds clubs hearts'.split()
    suits_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)

    def __init__(self):
        self._cards = [Card(rank, suit) for suit in self.suits
                                    for rank in self.ranks]


    def __len__(self):
        return len(self._cards)

    def __getitem__(self, position):
        return self._cards[position]


Here is how code is called to run;
Running:

>>> beer_dark = Card('7', 'diamonds')
>>> beer_card
Card(rank='7', suit='diamonds')

>>> deck = FrenchDeck()
>>> len(deck)
52

As the code grows, it either adds to the file or tweaks to the terminal. People, let me know if there is a practical way or need to adapt the code by incrementing parameters where there are not so that I can run it at once in the interpreter.

  • I won’t be able to elaborate the answer at the moment, but what you can do is create two files: one that defines the class, which you already have, and the other one where you will put the execution codes. To run, just run the execution file by the Python interpreter. So, any change in execution you can make by editing the file, without having to rewrite all the code in the terminal.

  • Use Linux, Mac or Windows?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.