Skip to main content

Get the Reddit app

Scan this QR code to download the app now
Or check it out in the app stores

r/learnpython

members
online

Ask Anything Monday - Weekly Thread

After close to a decade of using Python mostly for scripting, I am finally in a role where I get to design and write OOP code. I have seen the light, but...I still think procedural programming is easier to read. Is it just me? After close to a decade of using Python mostly for scripting, I am finally in a role where I get to design and write OOP code. I have seen the light, but...I still think procedural programming is easier to read. Is it just me?

I realize I'm still new to this, but after building my own project from scratch in a proper directory structure and with collaborators, I obviously see the benefits of OOP.

Still, people in tech often say "you read more code than you write", and so you should always try to write code as legibly and clearly as possible, and to that end, I still think procedural programming is way easier to read. Am I just being a little dense here?

To be clear I am not saying "therefore, we should all write ridiculously long scripts instead of neatly structuring modules into subfolders". Just observing that, although I now see why OOP came to be, I am still having difficulty with, say, going from 0 to 1 when it comes time to understanding how a given Python project works. Reading a long bash script is kind of like reading an ordinary book. Getting acquainted with a new project is kind of like reading a Choose Your Own Adventure book, except instead of following one storyline you're trying to follow all threads at once.


Help or advice with tip calculator? Help or advice with tip calculator?

So I've been off and on learning python for a while now, and now I'm trying to practice some online assignments (no I'm not in school this isn't me trying to get out of homework) and I'm stuck trying to figure out how to convert my strings to floats.

main function was pre-built for the assignment, all I've done was add the stuff below dollars_to_float and percent_to_float.

Strips are there because assignment said to remove the dollar and percent sign for the print so thats just what i did. sorry if its gross or something to the veterans who may be lurking

EDIT: It is worth noting that float(d) and float(p) likely as well throw errors saying the conversion from string to float can't be done and I've put them in multiple places to no avail.

def main():
    dollars = dollars_to_float(input("How much was the meal? "))
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")


def dollars_to_float(d):
    float(d)
    print(d.strip('$'))
    
    


def percent_to_float(p):
    float(p)
    print(p.strip('%'))
    
    

main()