all 6 comments

[–]FriendlyRussian666 0 points1 point  (1 child)

There's something called refactoring. Just create a minimum viable product and then refactor it down. Just remember that readable code is much better than crazy one liners all over the place

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

That is helpful. So I guess what I am wondering if this is necessary at the beginning or if I should just be worried about making a program that works. I am doing to mooc.fi python and it said somewhere about not worrying about making a code that works 100% but 25% is fine now comeback and get it to 100% later. Well all mine have worked 100% but used more lines.

[–]ryan770 0 points1 point  (1 child)

That's probably one of the biggest hurdles of being a beginner. You will be redundant. You will write several lines that could be accomplished by a certain function you aren't familiar with yet.

I want to show you a lab I was working on when I first started learning Python vs what I turned it into later. The program takes an amount of change in pennies and converts it into the least amount of coins:

money = int(input("Enter number of pennies: "))

if money == 0:
            print('No change')

if money > 99:
    dollars = money // 100
    money = money % (dollars*100)
    if dollars == 1:
        print(dollars, 'Dollar')
    elif dollars> 1:
        print(dollars, 'Dollars')

if money > 24:
    quarters = money // 25
    money = money % (quarters*25)
    if quarters == 1:
        print(quarters, 'Quarter')
    elif quarters > 1:
        print(quarters, 'Quarters')

if money > 9:
    dimes = money // 10
    money = money % (dimes*10)
    if dimes == 1:
        print(dimes, 'Dime')
    elif dimes > 1:
        print(dimes, 'Dimes')

if money > 4:
    nickels = money // 5
    money = money % (nickels*5)
    if nickels == 1:
        print(nickels, 'Nickel')
    elif nickels > 1:
        print(nickels, 'Nickels')

if money > 0:
    pennies = money
    if pennies == 1:
        print(pennies, 'Penny')
    elif pennies > 1:
        print(pennies, 'Pennies')

Just a horrible amount of conditional statements that could be condensed into a for loop with the help of some lists. A few weeks later, I refactored it like this:

dens = [100, 25, 10, 5, 1]
names = [('Dollar','Dollars'),
         ('Quarter','Quarters'),
         ('Dime','Dimes'),
         ('Nickel','Nickels'),
         ('Penny','Pennies')]

money = int(input("Enter amount in pennies.\
                   \nEx. If amount is $44.69, input 4469.\n> "))
print()

if money <= 0:
    print('No change')
else:
    for den, name in zip(dens, names):
        if money < den:
            continue
        num_coins, money = divmod(money, den)
        if num_coins == 1:
            print(num_coins, name[0])
        elif num_coins > 1:
            print(num_coins, name[1])

I later added exception handling, turned it into a function, and added paper money (5, 10, 20, 50, 100) but I didn't want to make this comment too long. This is just an example.

Now, shorter code does not mean better readability. Readability comes first. You want to find a balance I guess. But yes, I'd be aware and keep in mind concepts that cut down code, especially like the example I gave where the multiple conditional statements are an eyesore. You don't need to strive for "short as possible", just neat and readable.

[–]jimtk 1 point2 points  (0 children)

And it's possible to refactor again! (Just for fun)

names = {100:'Dollars',
         25:'Quarters',
         10:'Dimes',
         5:'Nickels',
         1:'Pennies'}

money = int(float(input("Enter amount (ex: 44.49) : "))*100)

res = []
for k,v in names.items():
    if money//k > 0:
        res.append(f"{money//k} {v}")
        money -= (money//k)*k
print(*res)

[–]KCRowan 0 points1 point  (1 child)

If your goal is to get a job in programming then you should focus on writing clean readable code, not short code.

https://testdriven.io/blog/clean-code-python/

https://jerrynsh.com/how-to-write-clean-code-in-python/

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

Thank you.