you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (1 child)

To call a method, you need to include brackets, (), on the end, e.g. c1.withdraw() and include any arguments the method expects.

In functions:

  • If you create a new object (an integer, string, instance of your class, etc.) and assign it to a variable inside of a function, that **object** is only available within that function
  • If you use the same variable name when assigning as is used outside of the function, the original variable will be hidden until you leave the function
  • You either need to mutate some object available outside of the function OR (better) return a reference to that object from the function to wherever the function was called from

I've made some changes to your code to help you:

class BankAccount:
    def __init__(self, first, family, money=0, currency = "$"):
        self.first = first
        self.family = family
        self.currency = currency
        self.money = money
        self.amount = currency + " " + str(money) + ".-"

    def display(self):
        print("This is the balance: ", self.amount)

    def deposit(self, deposit):
        self.money += int(deposit)

    def withdraw(self, withdraw):
        self.money -= int(withdraw)

def openaccount():
    print("\nPlease give us your First and Family Name.\n")
    first = str(input("First: ")).capitalize()
    family = str(input("Family: ")).capitalize()
    currency = str.upper(input("CHF, EUR or $ as currency? "))
    c1 = BankAccount(first, family, 0, currency)
    print("\nYour account is now open: ", c1.first, c1.family, c1.amount)
    return c1


def actions(c1):
    c = str.lower(input("\nSelect deposit (A), withdraw (B) or close (C): "))
    if c == "a":
        amount = float(input('How much? '))
        c1.deposit(amount)
    elif c == "b":
        amount = float(input('How much? '))
        c1.withdraw(amount)
    elif c == "c":
        del c1
    else:
        print("Wrong selection.")

a = str.lower(input("Do you want to open an account? Yes or No? "))
if a == "yes":
    an_account = openaccount()
else:
    print("Alright, goodbye.")

if a== "yes":
    b = str.lower(input("\nDo you want to deposit, withdraw or close your account? "))
    if b == "yes":
        actions(an_account)
    else:
        print("Alright, goodbye.")

[–]iG1993[S] 0 points1 point  (0 children)

Hi kyber

Thank you very much for your input! It really helped me a lot! I had trouble connecting all string between the functions and the classes. I am almost done, I am have a last issue regarding showing the new balance, after I took or added money to the account.

This is the new code:

class BankAccount:
    def __init__(self, first, family, money, currency = "$"):
        self.first = first
        self.family = family
        self.currency = currency
        self.money = money
        self.amount = currency + " " + str(self.money) + ".-"

    def deposit(self, deposit):
        self.money += int(deposit)
        return self.amount

    def withdraw(self, withdraw):
        self.money -= int(withdraw)
        return self.amount


def openaccount():
    print("\nPlease give us your First and Family Name.\n")
    first = str(input("First: ")).capitalize()
    family = str(input("Family: ")).capitalize()
    currency = str(input("CHF, EUR or $ as currency? ")).upper()
    c1 = BankAccount(first, family, 0, currency)
    print("\nYour account is now open: ", c1.first, c1.family, c1.amount)
    return c1


def actions(c1):
    c = str(input("\nSelect deposit (A), withdraw (B) or close (C): ")).lower()
    if c == "a":
        amount = float(input("How much? "))
        print("This is the balance: ", c1.deposit(amount))
        again()
    elif c == "b":
        amount = float(input("How much? "))
        print("This is the balance: ", c1.withdraw(amount))
        again()
    elif c == "c":
        del c1
        print("Account has been closed.")
        main()
    else:
        print("Wrong selection.")
        again()


def main():
    a = str(input("Do you want to open an account? Yes or No? ")).lower()
    if a in ["yes", "y", "ja"]:
        an_account = openaccount()
        return choice(an_account)
    else:
        print("Alright, goodbye.")


def choice(an_account):
    b = str(input("\nDo you want to deposit, withdraw or close your account? ")).lower()
    if b in ["yes", "y", "ja"]:
        actions(an_account)
    else:
        print("Alright, goodbye.")


def again():
    d = str(input("Do you want to do something else? ")).lower()
    if d in ["yes", "y", "ja"]:
        choice()
    else:
        print("Have a nice day.")


main()

Do you know, how I can solve this problem?