On 2017-02-07 07:34, Rafael Skovron wrote:
I'm trying to learn how to use Classes but I keep getting NameErrors no
matter what code I put into the script.

Any ideas why?

Assuming the code you've edited using vim is in a file mymodule.py
And after invoking the interpreter you issue the following:
import mymodule.py
your instantiation statement needs to be
rafael = mymodule.Customer('rafael', 100.0)

Alternatively you can use the following import statement (not generally recommended:)
from mymodule import Customer



My general workflow is I edit in vim, then invoke python3 interpreter,
import the module and try to use the Class and methods from the class.

For example, importing customer.py and assigning this object yields:

rafael = Customer('rafael',100.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Customer' is not defined



class Customer(object):
    """A customer of ABC Bank with a checking account. Customers have
the    following properties:
    Attributes:        name: A string representing the customer's
name.        balance: A float tracking the current balance of the
customer's account.    """

    def __init__(self, name, balance=0.0):
        """Return a Customer object whose name is *name* and starting
      balance is *balance*."""
        self.name = name
        self.balance = balance

    def withdraw(self, amount):
        """Return the balance remaining after withdrawing *amount*
   dollars."""
        if amount > self.balance:
raise RuntimeError('Amount greater than available balance.')
        self.balance -= amount
        return self.balance

    def deposit(self, amount):
        """Return the balance remaining after depositing *amount*
  dollars."""
        self.balance += amount
        return self.balance
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to