记分牌中的for循环无法正常工作

时间:2015-08-17 22:13:22

标签: python

我想创建一个记分牌,如果你搜索一个玩家,它会显示该玩家每个级别的结果。

但是,我无法让for循环正常工作。

我知道底部的印刷陈述有点奇怪,但它是需要显示的方式

level_1_score = 0
level_2_score = 0
level_3_score = 0
level_4_score = 0
level_5_score = 0
player_search = input("Please enter the name of the "
                      "player you would like to search for.")    

no_of_lines = player_list.readlines()
length_of_file = len(no_of_lines)

for i in range(0, length_of_file):
    for line in player_list:

        player_name,level,score = line.split(",")
        player_list.readline()
        if player_search == player and level == 1:
            level_1_score = score

        elif player_search == player and level == 2:
            level_2_score = score

        elif player_search == player and level == 3:
            level_3_score = score

        elif player_search == player and level == 4:
            level_4_score = score

        elif player_search == player and level == 5:
            level_5_score = score

print("the scores for" , player_search, "are:") 
print("  Game level          Score")
print("_" * 30)
print("    Level 1            ",level_1_score,"\n")
print("    Level 2            ",level_2_score,"\n")
print("    Level 3            ",level_3_score,"\n")
print("    Level 4            ",level_4_score,"\n")
print("    Level 5            ",level_5_score,"\n")

1 个答案:

答案 0 :(得分:3)

如果player_list是一个文件对象,那么你已经用.readlines()耗尽了迭代器,所以没有任何东西可以用for line in player_list:循环,如果你想要的话,你需要seek使用readline或迭代for line in player_list

for i in range(0,length_of_file):
    player_list.seek(0) # reset pointer
    for line in player_list:

我不确定player_list.readline()正在做什么,因为你没有使用返回的值,所以你可能只想删除它,你已经获得了for line in player_list的行,它可能会更多感觉只是迭代no_of_lines,这是所有行的列表:

for i in range(0,length_of_file):
    for line in no_of_lines:

使用for i in range(0,length_of_file):意味着你循环相同的行length_of_file次,你没有使用i所以可能只是忘记外部循环并循环遍历文件对象,忘记{{1这意味着你循环遍历文件中的所有行:

no_of_lines

您还要使用 for line in player_list: player_name,level,score = line.split(",") if player_search == player and level == 1: level_1_score = score 将字符串与int进行比较,因此请使用字符串代替所有比较and level == 1:...:...

您的所有代码都可以重写为:

and level == "1"

或使用csv模块读取文件并解压缩:

level_1_score = 0
level_2_score = 0
level_3_score = 0
level_4_score = 0
level_5_score = 0
player_search = input("please enter the name of the player you would like to search for")

# open file, with will automatically close it for you
with open("your_file") as f:
    # loop over every line
    for line in f:
        player, level, score = line.split(",")
        if player_search == player and level == "1":
            level_1_score = score
        elif player_search == player and level == "2":
            level_2_score = score
        elif player_search == player and level == "3":
            level_3_score = score
        elif player_search == player and level == "4":
            level_4_score = score
        elif player_search == player and level == "5":
            level_5_score = score

print("the scores for" ,player_search,"are:") 
print("  Game level          Score")
print("_"* 30)
print("    Level 1            ",level_1_score,"\n")
print("    Level 2            ",level_2_score,"\n")
print("    Level 3            ",level_3_score,"\n")
print("    Level 4            ",level_4_score,"\n")
print("    Level 5            ",level_5_score,"\n")

最后一个改变是使用dict来存储关卡,这样我们就可以删除所有的elifs:

import csv
with open("your_file") as f:
    # csv.reader will split your data for you 
    r = csv.reader(f)
    # unpack elements each row
    for player, level, score in r:
        if player_search == player and level == "1":
            level_1_score = score
        elif player_search == player and level == "2":
            level_2_score = score
        elif player_search == player and level == "3":
            level_3_score = score
        elif player_search == player and level == "4":
            level_4_score = score
        elif player_search == player and level == "5":
            level_5_score = score

然后使用范围为1-6的for循环,player_search = input("please enter the name of the player you would like to search for") import csv # store levels in a dict so we can avoid all the level logic checks levels = {"1": "0", "2": "0", "3": "0", "4": "0", "5": "0"} with open("your_file") as f: r = csv.reader(f) for player, level, score in r: if player_search == player: # use level as key to update the value in the dict levels[level] = score # break # uncomment if there can only be one name == to player_search 半开,所以我们得到1,2,3,4,5然后使用每个int强制转换为str来访问dict并得到每个级别的值,使用str.format格式化输出:

range

即使在您自己的代码中,重新检查print("The scores for {} are:".format(player_search)) print(" Game level Score") print("_"* 30) for i in range(1,6): print(" Level {} {} ".format(i,levels[str(i)])) 也是多余的,如果我们有匹配,我们需要检查的是级别:

if player_search == player: