5 Replies - 443 Views - Last Post: 05 October 2018 - 10:03 AM Rate Topic: -----

#1 warrenb17   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 10-July 17

Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 07:25 AM

just did an excercise

this was my code

user_name = input("What is your name?")
user_age = int(input("What is your age?"))
turning_100 = 2018-user_age+100
print("You will turn 100 in the year", turning_100)


this was the solution

name = input("What is your name: ")
age = int(input("How old are you: "))
year = str((2018 - age)+100)
print(name + " will be 100 years old in the year " + year)


are you better off casting or printing out variables the way i have? or does it really not matter?
also why is year = str((2018 - age)+100) written like that and my line here turning_100 = 2018-user_age+100
works exactly the same.

thanks for reading

Is This A Good Question/Topic? 2
  • +

Replies To: Do certain habits matter down the line,im guessing yes

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 462
  • View blog
  • Posts: 1,467
  • Joined: 27-December 13

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 07:42 AM

There is almost always more than one way to write a program.
In your two examples it is more a matter of taste, - not syntax or performance.

Only, you don't use the 'user_name' for anything in the first code.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7288
  • View blog
  • Posts: 15,169
  • Joined: 16-October 07

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 09:19 AM

Those are mostly stylistic differences. I wouldn't worry, either way. Your programming style will evolve over time as you figure out what works best for you.

I'd classify bad habits as structural, like globals or just too many bloody variables. These sort of habits are self correcting if you program long enough: you get better or you give up. A necessary, and rarely noted, quality in a programmer is a high tolerance for frustration. Anyone can write a program, it's debugging that is the real test.

There are as many ways to craft code as there are programmers. For the sake of argument, you could write the above as:
name = input("What is your name: ")
print("{0} will be 100 years old in the year {1}".format(name, (2018 - int(input("How old are you: ")))+100))


I don't think I like it, but it does save a couple of variable declarations.

I actually don't like that the magic number 100 appears twice and would be tempted to go with:
def show_future(target_age, name, age):
    year = (2018 - age) + target_age
    print("{0} will be {1} years old in the year {2}".format(name, target_age, year))

show_future(100, input("What is your name: "), int(input("How old are you: ")))



You could also parse out a calc_future... you get the idea. ;)
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2680
  • View blog
  • Posts: 10,715
  • Joined: 03-December 12

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 09:33 AM

year = str((2018 - age)+100)

This is just good practice to enforce mathmatic rules and since it will be a string, might as well make it one.

If you took the same thing, but needed to change the formula,
age = 12
year = (2018 - age)*100
print(year)
year = 2018 - age * 100
print(year)

Now we are way off.
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11265
  • View blog
  • Posts: 19,301
  • Joined: 19-March 11

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 10:00 AM

It's a good habit to be concerned about your habits :) If you want to expose yourself to some good habits, there are some well known books that you could take a look at. Clean Code by Robert Martin is one of them, Refactoring by Martin Fowler will expose you to a lot of good habits, and The Elements of Programming Style by (iirc) Kernighan and Plaugher is a classic.

In general, the guidelines for good habits are based in the observation that code is meant to communicate your intent. Things that communicate your intent, either to future-you or to other programmers who will read this, are good style.

The only specific style issue that I see here would be this:

print(name + " will be 100 years old in the year " + year)



which would be more idiomatically done with string formatting, like this:

print("%s will be 100 years old in the year %d" %(name, year))


or like this:

print("{} will be 100 years old in the year {}".format(name, year))


Was This Post Helpful? 2
  • +
  • -

#6 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2680
  • View blog
  • Posts: 10,715
  • Joined: 03-December 12

Re: Do certain habits matter down the line,im guessing yes

Posted 05 October 2018 - 10:03 AM

+ for showing both the old, and new formatting styles.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1