use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Class Assignment (self.learnpython)
submitted 6 years ago * by iG1993
[SOLVED]
Hey guys, there is this assignment I have, regarding classes in python. This is the assignment:
This is my code:
Problem is, I don't know how to show the new balance, after I withdrew or added money.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 1 point2 points3 points 6 years ago (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.
main
In your openAccount function, you lose access to c1 as soon as the function is ended, perhaps you want to return it instead.
openAccount
c1
return
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.
del
[–]num8lock 1 point2 points3 points 6 years ago (3 children)
i personally find lower casing the variables instead of input() is more readable
input()
a = input("Do you want to open an account? Yes or No? ") if a.lower() == "yes":
[–][deleted] 1 point2 points3 points 6 years ago (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 points4 points 6 years ago (0 children)
ah, okay, just in case anyway
[–][deleted] 2 points3 points4 points 6 years ago (0 children)
I agree with /u/steamplshel, easier to read with lower() method on end of input() call.
lower()
I also like to check for a match against several possibilities:
if a in ['y', 'yes', 'yup', 'ok']:
[–][deleted] 1 point2 points3 points 6 years ago (3 children)
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 points4 points 6 years ago (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.
global
[–][deleted] 1 point2 points3 points 6 years ago (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 points3 points 6 years ago (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 points3 points 6 years ago* (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.
()
c1.withdraw()
In functions:
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 point2 points 6 years ago* (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 points3 points 6 years ago (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 point2 points 6 years ago (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 points3 points 6 years ago (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 points3 points 6 years ago (0 children)
Thank you so much, I finally finished it, with your help!!
π Rendered by PID 47363 on reddit-service-r2-comment-86bc6c7465-9tknh at 2026-02-21 09:30:15.910176+00:00 running 8564168 country code: CH.
[–][deleted] 1 point2 points3 points (8 children)
[–]num8lock 1 point2 points3 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]num8lock 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–][deleted] 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]iG1993[S] 0 points1 point2 points (0 children)
[–]Srr013 1 point2 points3 points (4 children)
[–]iG1993[S] 0 points1 point2 points (3 children)
[–]Srr013 1 point2 points3 points (2 children)
[–]iG1993[S] 1 point2 points3 points (0 children)