문제

we are required to create a program that sets up the first round of a tournament for a project. If there is an uneven amount of contestants, the program is required to add a bye. Here is my code:

from random import*
bye=[]
teams=[]
while True:
    team=str(input("Enter the team names.(-1 to exit):\n"))
    teams.append(team)
    if team=="-1": break

if (len(teams))%2!=0:
    teams.append("bye")
print("          Tournament         ")
print("------------------------------")

shuffle(teams)
for i in range(len(teams)):
    print(team[i], team[i+1])

Errors?

도움이 되었습니까?

해결책

The error you have comes up in your method of printing out the results:

for i in range(len(teams)):
    print(team[i],team[i+1])

First of all, you have team instead of teams in the print statement, which is actually the string where you were storing user input, and should be '-1' by the time you're printing scores. You're getting the string index out of range error because it's trying to index the team string to the length of the teams list, which is likely larger than two.

Additionally, you're going to run into a similar problem with teams[i+1] since on the last iteration it will try and access one position beyond the length of the array. You're also going to be printing teams multiple times with how you have your loop set up, but I'll leave that to you to figure out.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top