Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using my Python program

Tags:

python

I'm really new to python and I have made the following program:

class AddressBook:
    def __init__(self):
        self.b = {}

    def insert(self,name, phone):
        self.b[name]=phone
        print "I am confused"

    def get(self,name):
        return self.b[name]

    def has_name(self,name):
        return self.b.has_key(name)

    def list(self):
        for n,p in self.b.iteritems():
            print n,p

    def delete(self, name):
        del self.b[name]

    def orderedList(self):
        orderedkeys = self.b.keys()
        orderedkeys.sort()
        for n in orderedkeys:
            print n, self.b[n]

I now want to compile it test it out in terminal to see if it all works. I went to the directory and compiled it with

python address.py

Now I want to add things to the list, print the contents of the list, delete them (pretty much play around with my program) but I don't know how...

After compiling, how do I manually test (play around) with my python program?

Thanks in advance.

like image 246
Adam Goldberg Avatar asked Nov 26 '16 15:11

Adam Goldberg


1 Answers

Python is an interpreted language, and .py files do not require direct compilation. There are a few ways to run Python code, but for "playing around" you can simply activate the Python interpreter and import the class.

In a command prompt:

> python

In Python:

>>> from address import AddressBook
>>> a = Addressbook()
>>> a.insert("Jenny", "867-5309")
>>> a.get("Jenny")
'867-5309'
like image 121
pylang Avatar answered Sep 29 '22 02:09

pylang