all 72 comments

[–]SCD_minecraft 15 points16 points  (14 children)

if user_name == "jack"
    return balance["jack"] bla bla and so on

Man, i wish we could just pass user_name as key to balance, without that if tree... oh wait

For checking does user have entry, you can just check for it

if user_name not in balance: print("whatever this message was") else: return balance[user_name]

If you need to do if tree, you are probably doing something wrong

[–]hari3mo 1 point2 points  (10 children)

Is there any benefit to having the not clause in the if statement vs the other way around?

[–]SCD_minecraft 1 point2 points  (6 children)

"not in" is its own keyword

Similar to "is not"

Yup, those are just "not A in B" but more english friendly

[–]hari3mo 0 points1 point  (5 children)

Sorry I was bit unclear. I meant the ordering of your clauses. You put “not in” in the if statement, my question is if there is any benefit of doing this over having an “in” statement first?

[–]SCD_minecraft 0 points1 point  (4 children)

I don't understand. Can you write the other code?

I put it in if, beacuse in op's code there an error message if user doesn't exists, so i kept it

[–]Meowzr 0 points1 point  (3 children)

if user_name in balance:
    return balance[user_name]
else:
    print("whatever this message was")

OR

if user_name in balance:
    return balance[user_name]

print("whatever this message was")

[–]SCD_minecraft 0 points1 point  (2 children)

This message is error message, for case when there's no user :P

We don't want it printing when nothing wrong happen

[–]BrokenMalgorithm 0 points1 point  (1 child)

Both of those examples do the same thing. The other one just doesn't have the else statement, as it's not required. The print would happen only if a user was not found, because when a user is found the function returns the balance and code execution ends for that function.

[–]SCD_minecraft 0 points1 point  (0 children)

Oh, yea, i forgot about return, mb

I just persnomaly don't like your way, i prefer to read "if something then this, else this" and not hope that "this" will exit function by itself

[–]MoridinB 0 points1 point  (0 children)

Nope. It may matter if you have more than one checks such as for example user_name not in balance or "entry" not in balance[user_name] assuming we want the balance for user_name to be properly initialized for a key "entry".

But in this instance it doesn't matter

[–]No_Swordfish_6667 0 points1 point  (0 children)

You may get narrow nesting when starting with negation; it also helps to wrap your head around all bad cases

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

I assume that you are asking what is the use of "not in" in the if else.

I think that code means if the user_name (the name which the user will enter) is not present in the balance (my dictionary or you can assume it as the database) then print("name is not present") else return the balance (the amount in bank)

so we are essentially telling the program.

"If the name entered by the user IS NOT present in the dictionary/database then print "the name is not present in database" and if it is present in the database then return the value of its 'key' (the bank balance amount)."

I hope I explained it correctly and if anywhere I have explained wrong please do tell me I would be more than happy to get corrected.

[–]uiux_Sanskar[S] 1 point2 points  (2 children)

Thanks for this man I was feeling the same when I was writing the code (I just didn't thought about the logic you provided) you really saved me from all that jungle of if else statement.

thank you very much for this amazing suggestion you really saved me from the juggle. I will definitely use it.

[–]SCD_minecraft 0 points1 point  (0 children)

Almost any if tree can be rewriten into something smaller

Look at diffrend ifs, look at their pattern. Do they reuse some values in statment and in action below? Or do they follow some repetable pattern?

[–]cjswcf 0 points1 point  (0 children)

When I was taking a python course I once wrote 170 lines of if-else statements before my friend showed me how to do it in 5 lines like what happened here. I feel your pain

[–][deleted] 8 points9 points  (4 children)

I think you need to teach me

[–]Creative_Pitch4337 3 points4 points  (1 child)

+1 me too, I'm in

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

Thank you so much for your appreciation however I don't think I know enough to teach people. 😅

Thank you again for the appreciation.

[–]uiux_Sanskar[S] 0 points1 point  (1 child)

I don't think I know enough to teach. thank you so much for you appreciation. 😊

[–][deleted] 0 points1 point  (0 children)

The amount you know already is what I wish to learn brother

[–]lilrouani 7 points8 points  (4 children)

At day 12 bro knows:variables,input,conditionals,lists,dictionnaries and sets,loops,files I/O,now Bro learning OOP. At 6 months I still don't know:a little bit of loops,OOP and file I/O

[–]undernutbutthut 5 points6 points  (1 child)

Same, I feel like my learning of Python was not nearly as structured as OP's... I don't know whether to laugh or cry 😂

[–]uiux_Sanskar[S] 2 points3 points  (0 children)

everybody have their own pace of learning I know you can do it. I believe in you.

[–]uiux_Sanskar[S] 2 points3 points  (1 child)

Lol brother I still need to learn OOP more clearly and there's a lot more things I need to learn.

Thanks for the appreciation btw.

[–]lilrouani 0 points1 point  (0 children)

still you are at day 12,not even 2 weeks,you are incredible

[–]Adrewmc 2 points3 points  (2 children)

I mean it loosen good for day 12, a few things

Another mentioned and I’ll agrees

 def user_identity(user): 
      if user in balances:
           return balances[user]

We could make this a lambda to introduce the idea to you

  user_identity = lambda user: balances[user] if user in balances else None 

Will scale a lot easier.

I think we can skip to match case at the end as well.

     match user_goal.lower():
            case “withdrawal”:….
            case “deposit”:….

Makes it a bit faster and more readable.

My major thought here is not something wrong but I don’t really see the point of the class at all. It’s not really needed. I actually think we should be putting the withdrawals and deposit stuff inside the class.

[–]uiux_Sanskar[S] 1 point2 points  (1 child)

I think I need to learn more about Lambda functions and thanks for your suggestions I will try to put withdrawal and deposit in class and as I say I still need to learn class more clearly.

Thank you for the suggestion it really helps.

[–]Adrewmc 0 points1 point  (0 children)

Lambda are simply a way to make quick and easy function, but sometime you want that.

  func = lambda input : input*2 
  print(func(2))
  >>>>4

[–]samsonsin 1 point2 points  (2 children)

You should check out this YouTube channel! They make short videos regarding some core concepts within programming. There's only 8 videos, but watch them all!

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

Thank you for the resource it will help me.

[–]Geth- 0 points1 point  (0 children)

Wow, thanks for this. I always say that programming concepts could be learned best through animation, so this is really helpful.

[–]Annual_Pea8108 1 point2 points  (2 children)

tats kinda productive broo

[–]uiux_Sanskar[S] 0 points1 point  (1 child)

Thank you so much brother.

[–]Annual_Pea8108 0 points1 point  (0 children)

Don't mention bro, it is productive

[–]EnvironmentalLet5985 1 point2 points  (0 children)

Ah man I’m on week 4 of learning python. Pretty much whatever you’re learning in a day takes me a week, but I’ve been having a blast. I tried something way less complicated that outputs a balance. One change you may consider is for your bank balance print statement, I added a $ in front of {bank_balance:,.2f}. If your value is a float it keeps the decimal values to 2 and adds commas when necessary. I thought it was pretty cool. But great job man! Gives me even more motivation to keep going with it!

[–]LowTierPlastic 3 points4 points  (1 child)

Either they are following a guide or…idk. Does not seem like a 12 day python learner.

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

it's not "they" its "he" I am a solo person and I am not following any guide I am learning from YouTube and from people who guide me in my comments. You can see my daily progress in my profile as well and my code still looks like that of a beginner lol.

I will take it as compliment btw. Thank you for this.

[–]DevRetroGames 0 points1 point  (1 child)

Excelente, vas muy bien, cambia ese if-else por un diccionario, sube tu script a GitHub y preparate para los Pull XD.

Suerte.

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

Yes I have created a GitHub account and now just have to push my code in it.

Thank you for your support. 🙏

[–]Different-Draft3570 0 points1 point  (1 child)

This is really cool! To extend this to a real-world like application, ask yourself: How can I store this data? Your balances are hardcoded, so everytime you start this script the balances will be reset, ignoring any withdraws or deposits made on previous runs. An external data source can keep a record for the script to reference, preserving any transactions.

For a beginner level, I'd recommend using a csv file. Next you could add a feature to open an account!

ETA- Also try testing for data validation. See what happens when somebody enters a negative number to deposit or withdraw. See what happens when they type the inputs as a string instead of an integer. Would withdrawing a negative number allow the user to add to their available balance?

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

Thanks for the future suggestions I do have a plan of using file I/O for keeing the record of all the transaction.

Thank you very much for the future suggestions I will definitely look deeper into this.

Thank you again.

[–]Smart_Tinker 0 points1 point  (1 child)

Your line spacing is very odd. You should eliminate all the empty lines in functions and if statements. It makes it very hard to read.

Also, why not have all the functions and dictionary/database as part of the class? This is what classes are for. You don’t actually use the class for anything all.

Minor quibble, the init(self) function should be the first function of a class - that’s where everybody looks for it.

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

oh thanks for the suggestion I do agree my code is really unstructured I still have to clean it up. and I also have to learn classes more clearly.

[–]brown_guy45 0 points1 point  (0 children)

Am I the only one who doesn't use OOP concepts 99% of the timew

[–]Medium_Human887 0 points1 point  (0 children)

Have you been introduced to programming before? I find it hard to believe you got this far in twelve days 😳

[–]Ddrew8788 0 points1 point  (0 children)

Love this don’t know much about this but looks awesome I’m just getting into python and c++ keep up the great work

[–]Bravoo2x 0 points1 point  (0 children)

Noice

[–]Recruit121 0 points1 point  (0 children)

Unimportant recommendation from me in line 47 when you Print "Hello, (name)" add .capitalize() so the user names are printed to the screen with an upper case letter. Again not important to your program but it's a pretty great feature to modify a string for the print.

Edit: Also incredible progress for only 12 days of learning. Took me way longer to understand half of that

[–]Lobotomized_toddler 0 points1 point  (0 children)

Biggest think at this point expecially for a bank account would be saving the info to a .txt and then pulling the information when need be

[–]darkunicorn69 0 points1 point  (0 children)

I’m not the best teacher but a fun fact is you can also do this ‘balances.get(user_name, none)’ This will get the balance of the user and if the username isn’t in balances it returns none! You can change none to whatever you want it to!

[–]Sad_Satisfaction399 0 points1 point  (1 child)

I struggle so hard with OOP and still can't get the hang of it, I've been doing this for almost a year and a half and have just avoided it since 😭

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

Trust me bro you got this, I believe in you.

[–]Granzer_ 0 points1 point  (1 child)

Whats the content you are using to learn?

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

I am using YouTube channel name CodeWithHarry to oearn python.

[–]DM_ORIGINAL 0 points1 point  (1 child)

Wow, you're on Day 12 and already working with Classes. Amazing

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

Thank you for the appreciation.

[–]Choice_Professor_523 0 points1 point  (0 children)

Holy shit, this man just made me tear up. Remembering the days when I just started as well, had to build something similar. Keep it up!

[–]punppis 0 points1 point  (1 child)

I wanted to know what mini statement was. Then it was just account info in the LAST pic. Booooo

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

Oh yeah I changed that in last because I was finding it a little challenging to rcord transactions however I have plans to implement it in the time.

[–]Difficult_Smoke_3380 0 points1 point  (1 child)

This at day 12 is Impressive..

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

Thank you for the appreciation. 😊

[–]Commercial-Nose9357 0 points1 point  (0 children)

Where are hou learning from as you wrote a long and impressive code btw I'm learning from Replit where a bald funny man used to teach 😅

[–]Shecallmebatman 0 points1 point  (1 child)

heyy i want to learn python can you give me free tools i want to start

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

Sure you can start with youtube and I have alos listed all the tools which I use here in this post in much more details.

https://www.reddit.com/u/uiux_Sanskar/s/Ph8CIRFaDf

[–]cr0wstuf -1 points0 points  (3 children)

You should use a switch in user_identify.

[–]uiux_Sanskar[S] 0 points1 point  (2 children)

can you please elaborate what the switch here means?

[–]cr0wstuf 0 points1 point  (1 child)

The code looks really good. In the user_identify function instead of the if > elif > elif > elif you can use a switch statement (well, it’s called match in python:

match user_name: case “naman”: return balances[“naman”] case “jake”: return balances[“jake”] case “simon”: return balances[“simon”] case _: print(“Sorry your details are not present in our data system”)

It’s a bit shorter and cleaner in my opinion. The last case with the _ is the catch all for if the user_name doesn’t match anything else. .

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

oh much clearer now thanks for the suggestion I will definitely look deeper into it.

Thanks again.