-1

I'm new to python...so i was trying to run this code but the error says menu() missing 1 required positional argument: 'self' after the while statement. However, when i added self the error says name 'self' is not defined. What is the proper way to run this code?

class Account:
    def __init__(self):
        self.balance = 1000
    
    def menu(self):
        print("Options:")
        print("1. Make Deposit")
        print("2. Make a Withdrawl")
        print("3. Obtain Balance")
        print("4. Quit")        

    def deposit(self):
        depositAmount = float(input("Enter amount of deposit: "))
        print("Deposit processed")
        self.balance += depositAmount
    
    def withdrawl(self):
        withdrawalAmount = float(input("Enter amount of withdrawl: "))
        if withdrawalAmount > 1500:
            print("Denied. Maximum withdrawl is $1,500.00")        
        elif withdrawalAmount > self.balance:
            print("Insufficient balance")
        else:
            self.balance -= withdrawalAmount
        
    def balance(self):
        print(self.balance)
    
    def exit(self):
        quit()
    
def main():
    while True:
        Account.menu()
        choice = int(input("Make a selection from the options menu: "))
        
        if choice == 1:
            Account.deposit()

        elif choice == 2:
            Account.withdrawl()

        elif choice == 3:
            Account.balance()
        
        elif choice == 4:
            quit()
            break

if __name__ == "__main__":
    main()
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Abood
  • 3
  • 2
  • 4
    You need to create an `Account` instance before calling the methods. For example: `account1 = Account()`. Then use `account1.deposit()` etc... – Mark May 24 '21 at 19:03
  • 1
    Why is this tagged `callback`? – Scott Hunter May 24 '21 at 19:06
  • 3
    Does this answer your question? [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – 8349697 May 24 '21 at 19:06
  • I'm sorry, but it seems like you fundmentally misunderstand how to use classes. I suggest looking at the official [tutorial on the subject](https://docs.python.org/3/tutorial/classes.html). Suffice it to say, you need to create an instance and use those methods on the instance. Just like `list` objects. `list` is just a class. When you do `mylist = list()` you are instantiating a list object. Then when you use `mylist.append(42)`, you are using the `list.append` method on the list instance. – juanpa.arrivillaga May 24 '21 at 19:12
  • *Be carefule*. your `Account` class has an instance variable, `self.balance`, but you also define a method, `def balance(self)`, that means your instance variable will *shadow* your method. don't name methods the same name as instance variables – juanpa.arrivillaga May 24 '21 at 19:13

1 Answers1

0

Try like this. You need to create an instance of the Account class. You also need to change the balance method's name because you already have "self.balance"

`class Account(): def init(self): self.balance = 1000

def menu(self):
    print("Options:")
    print("1. Make Deposit")
    print("2. Make a Withdrawl")
    print("3. Obtain Balance")
    print("4. Quit")        

def deposit(self):
    depositAmount = float(input("Enter amount of deposit: "))
    print("Deposit processed")
    self.balance += depositAmount

def withdrawl(self):
    withdrawalAmount = float(input("Enter amount of withdrawl: "))
    if withdrawalAmount > 1500:
        print("Denied. Maximum withdrawl is $1,500.00")        
    elif withdrawalAmount > self.balance:
        print("Insufficient balance")
    else:
        self.balance -= withdrawalAmount
    
def showBalance(self):
    print(self.balance)

def exit(self):
    quit()

account = Account()

def main(): while True: account.menu() choice = int(input("Choose an option: "))

    if choice == 1:
        account.deposit()

    elif choice == 2:
        account.withdrawl()

    elif choice == 3:
        account.showBalance()
    
    elif choice == 4:
        quit()
        break

if name == 'main': main()`

Costa
  • 16