简体   繁体   中英

How to extract numbers from csv file into variable

I want to make a highscore in a game on python - todo this i've realised I have to use an external file. I want it to extract the numbers located in the file and save them as a variable. It will then compare it to the users score and if it's bigger it will write the new high score to the file

I've been googling for days with no luck.

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

I want it to do above

I personally prefer to use dataclass CSV whenever I work with CSV files. You just need to type the column as integer (you do that by typing dataclass attribute as int )

It is a nice clean way to convert CSV rows to python objects. The only restriction is that you need to use python3.7 or higher.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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