Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

style or format question regarding input as integers

I want to develop good habits upfront.

Coders are always muttering about poorly structured code, so I am hypersensitive to not only learning to code, but wanting to learn it the right way up front to avoid being the person others are muttering about. :-)

In the team.py exercise, there are a few times where I got the correct result but it was written differently from the teacher's solution. I appreciate feedback on these two examples on which is the more acceptable or preferred style. They both work.

Thanks for the replies and helping me develop the best coding behaviors upfront... Donne

First Comparison: (comment - I took to heart the DRY approach, maybe too literally?)

MINE:

while True:
    add_player = input("\nWould you like to add a player to the list?  (Yes/No)    ")
    if add_player.lower() == "no":
        break
    else:
        name = input("Enter the name of the player to add to the team:    ")
        players.append(name)

TEACHER:

add_players = input("Would you like to add a player to the list?  (Yes/No)    ")
while add_players.lower() == 'yes':
    name = input("\nEnter the name of the player to add to the team:    ")
    players.append(name)
    add_players = input("Would you like to add a player to the list?  (Yes/No)    ")

Second Comparison: (comment - I didn't see a need to create a function, seemed like something else to maintain, but I could have used it.)

MINE:

for number in range(len(players)):
    print("Player", number+1, ":  {}".format(players[number]))

TEACHER:

player_number = 1
for player in players:
    print("Player {}: {}".format(player_number, player))
    player_number += 1

Third Comparison: (comment - this is a question of pure correctness. is it better to include int on the input line? Would it be easier for others who come after me to find and maintain it on the second line?)

MINE:

keeper = int(input("\nPlease identify the goal keeper by selecting the player number. 1-{}:  ".format(len(players))))

TEACHER:

keeper = input("Please select the goal keeper by selecting the player number. (1-{})".format(len(players)))
keeper = int(keeper)

1 Answer

Hi, for the first comparison i prifer the teachers for the reason he used less ones of code and i think that the way he’s checking if the user entered yes or no is better. , second comparison i used advance syntax, if u know what ur doing and it’s not confuse u go for it, he used his way in order to be as clear as possible. for the last one i prifer ur approach, more clean and u achieve the same things. i’m not by no means an expert lol, but as a student that is my thoughts.