简单python程序-卡住
我在写这段代码,上面写着我放的人是否有高分 当我在列表中输入一个人,例如“Thor”时,效果很好,但是程序没有捕捉到错误,如果该名字不在列表中,也没有打印“不,他们没有高分”简单python程序-卡住,python,list,integer,range,Python,List,Integer,Range,我在写这段代码,上面写着我放的人是否有高分 当我在列表中输入一个人,例如“Thor”时,效果很好,但是程序没有捕捉到错误,如果该名字不在列表中,也没有打印“不,他们没有高分” Names = ['Ben', 'Thor', 'Zoe', 'Kate'] Max = 4 Current = 1 Found = False PlayerName = input("What player are you looking for? ") while (Found == False) and (Curre
Names = ['Ben', 'Thor', 'Zoe', 'Kate']
Max = 4
Current = 1
Found = False
PlayerName = input("What player are you looking for? ")
while (Found == False) and (Current <= Max):
if Names[Current] == PlayerName:
Found = True
else:
Current = Current + 1
if Found == True:
print("Yes, they have a top Score")
else:
print("No, They do not have a top score")
我怎样才能解决这个问题?谢谢。 < p>您从“代码> 1”/代码>开始了数组索引。而在Python、C、C++和许多语言中,数组索引(如列表)从<代码> 0 <代码>开始,并在<代码>数组长度-1 < /代码>中结束。将循环更改为:
while (Found == False) and (Current < Max):
↑
while(Found==False)和(Current
最大值应为3而不是4。
不要忘记数组是0索引的,所以Names[3]
引用第四个元素(也是最后一个)
从0Current=0
开始,列表以零为基础,更改为Current
,您将在0、1、2、3(总共4个元素)上运行。谢谢它工作得非常好,我该怎么办?结束问题还是?@marounnaroun我只是看到你在评论中回答问题的时间比我早。所以我删除了我的答案。:)请把它寄出去。这是你的:)@Abraham没关系,你撤销了删除:)你发布了一个很好的解释。如果PlayerName in Names:
比while循环更简洁。
while (Found == False) and (Current < Max):
↑