我对python比较陌生,目前正在网上找到一个学校项目。当我运行这个代码时,它应该计算阵列中出现“Action”和“Sport”的次数。阵列搜索不工作 - 不知道为什么?
forename = ["Joe", "George", "Oliver"]
HistoryGenre=[["Action", "Action", "Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport"], ["Sport", "Sport", "Sport", "Sport", "Action", "Action", "Sport", "Sport", "Sport", "Action"], ["Action", "Action", "Sport", "Sport", "Action", "Action", "Action", "Sport", "Sport", "Sport"]]
rr=1
ActionCounter=0
SportCounter=0
while rr==1:
rec=input("Who would you like to recommend games for?")
if rec in forename:
rr+=1
r=forename.index(rec)
RepeatIndex=0
for i in HistoryGenre[r]:
if HistoryGenre[r:RepeatIndex]=="Action":
ActionCounter+=1
RepeatIndex+=1
else:
SportCounter+=1
RepeatIndex+=1
if RepeatIndex==9:
print(ActionCounter)
print(SportCounter)
当我运行这段代码ActionCounter打印为0和SportCounter打印为9.我不明白怎样和为什么发生这种情况考虑输出应该是7和3就非常有可能更初学者错误。
值哇非常感谢你这固定我的问题。我似乎没有意识到搜索数组的恰当语法。然而,我仍然不知道为什么ActionCounter的输出为0,为什么它是9的SportCounter。另外,感谢你教我关于我从未使用过的字典。 –
@OliD HistoryGenre [r:RepeatIndex] ==“Action”从来都不是真的,所以它总是会跳过它并转到else:SportsCounter + = 1。 –