繁体   English   中英

如何从 csv 文件中提取数字到变量中

[英]How to extract numbers from csv file into variable

我想在 python 上的游戏中获得高分 - 为此我意识到我必须使用外部文件。 我希望它提取文件中的数字并将它们保存为变量。 然后它将它与用户分数进行比较,如果它更大,它将新的高分写入文件

我已经用谷歌搜索了几天没有运气。

while theo >= 0 and theo <=9:
    theo = theo + 1
    x = (random.randint(0, 12))
    y = (random.randint(0, 12))
    print("Your sum is: " ,y,"x ",x)
    answer = input("What is the answer?: ")
    while len(answer) == 0:
        answer = input("What is the answer?: ")
        an = (x*y)             
    if int(answer) == (an):
        print("Well done - you were correct ")
        score = score + 1

    elif int(answer) != (an): 
        print("Sorry, you were wrong. The correct answer was " ,an)
    if theo == (10):
        print("The quiz has finished. your score is ",score)
        again = input("Do you want to play again? (Y/N)")
        gamesplayed = gamesplayed + 1
        if again == ("Y") or again == ("y"):
                theo = 0
                timesplayed = timesplayed + 1
        elif again!=("y") or again!=("Y"):
            print("well done your final score was ",score," you played ",timesplayed," times") 

我想让它在上面做

每当我使用 CSV 文件时,我个人更喜欢使用数据类 CSV 您只需将列键入为 integer (您可以通过键入 dataclass 属性为int来做到这一点)

将 CSV 行转换为 python 对象是一种非常干净的方法。 唯一的限制是您需要使用 python3.7 或更高版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM