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 Challenge Solution

Joseph Godin
Joseph Godin
3,185 Points

This one is too tough honestly

I got as far as the while statement and used an if statement to create the loop but couldn't rmbr how to tie in the list into it. I ended up just following along and doing the exact code the teacher has. The code still doesn't even work correctly. I wish there were more practice coding things like this... I want to get better but throwing so many different parts into one challenge really makes it difficult.

5 Answers

Joseph Godin
Joseph Godin
3,185 Points

Okay I watched some Corey Schafer youtube videos and it helped clear up some concepts. I am still getting tripped up on the goalkeeper aspect though and i think it has something to do with me doing the code differently than the example.

proceed=input("Would you like to add a player to the roster? Yes/No  ")
while proceed.lower() == "yes":
    if proceed.lower() == "yes":
        name=input("Type the name of the player.  ")
        players.append(name)
        proceed=input("Would you like to add a player to the roster? Yes/No  ")
    if proceed.lower() == "no":
        print("\nThere are {} players on the team.".format(len(players)))

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

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

any advice on how to get the goalkeeper integer to work on player so i can wrap this project up?

Instead of goalkeeper=(int(player)), you could get the player with the integer value of goalkeeper by using players[int(goalkeeper)].

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Just keep practicing until it sticks. It's okay that you don't get everything right away even in these practice sessions. Remember these practice workshops are designed to help you test many solutions to a single problem using the knowledge you've learned from previous courses so there's plenty of content for you to go on.

Try going through the practice again. And if you're following along take it step by step so you can get a handle on what each part of the code is doing. Code can look scary but if you try to remember concepts rather than memorising code that's a better way to look at it I think.

Post back the code you're trying if you still get stuck. Use the markdown cheatsheet on your page if you want to know how to format code in the forums. :)

Szymon Dabrowski
Szymon Dabrowski
2,207 Points

Don't give up. You are certainly not alone. I am in the same position as you are at the moment but eventually it will stick and we can then move onto something new and repeat the same steps haha

Joseph Godin
Joseph Godin
3,185 Points

Thanks. I think I will continue on and retry that practice again later in the week to continue trying to memorize and retain these concepts. Thanks for the encouragement.

Raj Maraj
seal-mask
.a{fill-rule:evenodd;}techdegree
Raj Maraj
Data Analysis Techdegree Student 5,377 Points

I struggled wayyyyy too hard on this practice quiz as well. I was in the same position, I could not wrap my head around where player came from.

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

In the code, the line for player in players: is a loop construct in Python that iterates over each element in the list players. Here's a detailed explanation of what it does:

for: This keyword starts a loop in Python.

player: This is a variable that will take on the value of each element in the list players as the loop progresses.

in: This keyword indicates that you are iterating over a sequence or collection.

players: This is the list over which you are iterating.

So, for player in players: means "for each element in the list players, execute the following block of code". During each iteration of the loop, the variable player will be assigned the next element in the list.

player: During each iteration, player takes the value of the current element from the players list.

print("Player {}: {}".format(player_number, player)): This line prints the player's number and name.

player_number += 1: This increments the player_number by 1 for the next iteration.

For example, if players list contains ['Alice', 'Bob', 'Charlie'], the loop will run three times:

player will be 'Alice', player_number will be 1. player will be 'Bob', player_number will be 2. player will be 'Charlie', player_number will be 3. This loop allows you to print each player's number and name in a formatted string.