Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比赛项目_Python_List_Tournament - Fatal编程技术网

Python 比赛项目

Python 比赛项目,python,list,tournament,Python,List,Tournament,我们需要创建一个程序,为项目设置第一轮比赛。如果参赛者数量参差不齐,则该计划需要添加一个“再见”。这是我的密码: 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.appe

我们需要创建一个程序,为项目设置第一轮比赛。如果参赛者数量参差不齐,则该计划需要添加一个“再见”。这是我的密码:

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])

错误

打印结果的方法中出现错误:

for i in range(len(teams)):
    print(team[i],team[i+1])
首先,在print语句中有
team
而不是
teams
,它实际上是存储用户输入的字符串,在打印分数时应该是
'-1'
。您得到的
字符串索引超出范围
错误是因为它试图将
团队
字符串索引到
团队
列表的长度,该长度可能大于2


此外,您将在
团队[i+1]
中遇到类似的问题,因为在上一次迭代中,它将尝试访问超出数组长度的一个位置。您还将多次打印团队关于如何设置循环的信息,但我将让您自己来解决。

“错误?”您为什么不亲自运行并查看?这就是为什么您有一个python解释器。另外,如果这不是java,请不要标记它
java
,我已经运行了它,并且错误字符串索引超出了范围。我似乎找不到其中的错误,因为我是一名高中生,目前正在学习python和编程的基础知识。你可以查看我制作的插件,它包含了你想要的所有逻辑: