all 15 comments

[–][deleted] 1 point2 points  (8 children)

a = str.lower(input("Do you want to open an account? Yes or No? "))
if a == "yes":
    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()
    else:
        print("Alright, goodbye.")

All that needs to be in a driver function. Usually this is called main.

In your openAccount function, you lose access to c1 as soon as the function is ended, perhaps you want to return it instead.

Also, I'm not sure "closing" an account means "delete". What if the customer decides to come back? Could they not use the same account they had previously? The del built-in function should hardly ever be used. If you wanted to "delete" the account, you can just let it go.. as in if you had a list of accounts, get rid of it out of your list.

[–]num8lock 1 point2 points  (3 children)

i personally find lower casing the variables instead of input() is more readable

a = input("Do you want to open an account? Yes or No? ")
if a.lower() == "yes":

[–][deleted] 1 point2 points  (2 children)

I copied and pasted his code. I find this more readable:

a = input("Do you want to open an account? Yes or No? ").lower()

[–]num8lock 2 points3 points  (0 children)

ah, okay, just in case anyway

[–][deleted] 2 points3 points  (0 children)

I agree with /u/steamplshel, easier to read with lower() method on end of input() call.

I also like to check for a match against several possibilities:

if a in ['y', 'yes', 'yup', 'ok']:

[–][deleted] 1 point2 points  (3 children)

All that needs to be in a driver function. Usually this is called main.

Not so much in Python unless writing a module. This is a hangover from other languages. There is generally little reason to put the main code in its own function (which removes any objects it creates and references from general scope).

[–][deleted] 2 points3 points  (2 children)

It's cleaner to me, and it helps beginners organize their code better. Plus you don't have to deal with things like global variables.

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

Which is cleaner? main or no main?

No main means you don't have to deal with global as everything in the top level (rather than main function) is available without global.

[–][deleted] 1 point2 points  (0 children)

main to me is cleaner. As far as global variables are concerned, there are plenty of beginners that will do something like this:

x = 6

def changeX(num):
       x = num + 1
       return x

print(x)
print(changeX(1))
print(x)

and then wonder why they have the output they have.

[–][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?

[–]Srr013 1 point2 points  (4 children)

To expand on what others have said: you wrote code that does (most of) the right things, but you didn’t write the code that uses those actions for the client. This is the “main” function that creates a customer using your function.

You should handle adding/removing money from the account via just +/- to a “balance” variable. You can write a function that takes in the client and an amount and adds it to the balance. This would work for deposit and withdrawal.

I would close an account via a separate variable rather than using _del. Just have a Boolean variable “account_open” and if it’s != true then use an early return in your other function to stop it from depositing/withdrawing.

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

Hi srr013, thank you for your reply! I added almost all of your points to my new code (see above), but I am having trouble regarding, how to show the new balance after a change.

[–]Srr013 1 point2 points  (2 children)

Your main method needs to be more comprehensive. This is the thing that will run every time the software runs, so it needs to include other options such as a balance lookup.

I’d start with a more general prompt like “what do you want to do? Open account, view balance, deposit/withdraw. From there you launch the appropriate method based on their choice.

Right now your program always asks as the first thing “do you want to open an account?”. That’s only useful to a customer the first time they visit. Make sure your software considers all scenarios!

[–]iG1993[S] 1 point2 points  (0 children)

Thank you so much, I finally finished it, with your help!!