试图将一个整数输入一个文件并将其作为一个检索.蟒蛇 3x [英] Trying to input a integer into a file and retrieving it as one. Python 3x

查看:23
本文介绍了试图将一个整数输入一个文件并将其作为一个检索.蟒蛇 3x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在努力学习 Python,我有这个小应用程序来进行英语测试.但我想将分数"放入一个文件中,但不是作为字符串而是作为整数.那我也想把它拿出来.

so I've been trying to learn Python and I have this little app to conduct an English test. But I want to take the 'score' and put it into a file but not as a string but as an integer. Then I also want to take it out as one.

此外,如果名称"是新的,我有时会创建文件,所以我想检查文件是否为空,如果是,则在其中放一个 0.有什么提示吗?

Also along the way I sometimes will create files if the 'name' is new so I want to check if the file is empty and if so to put a 0 in there. Any tips?

当然,如果您有任何其他批评,我很乐意听到:)

And of course if you have any other criticism I would be happy to hear it :)

questions = ["1. You won't find Jerry at home right now. He ______________ (study) in the library."
        "2. Samantha ______________   (do) her homework at the moment."
        "3. We______________  (play) Monopoly a lot."
        "4. Ouch! I ______________ (cut, just) my finger!"
        "5. Sam (arrive) ______________in San Diego a week ago."]
keys = ["is studying"
    "is doing"
    "play"
    "have just cut"
    "arrived"]

print("Hello. This is an English test.")
name = input("What is your name?")

#creates a file with the person's name
file = open("%sScore.txt" % name, "w+")

#reads that person's score
score = file.read()

#if there is nothing in the file it puts in 0
if (len(score) == 0):
file.write(bytes('0'), "UTF-8")

print("Type in the verb given in brackets in the correct form.\n")

#this loop asks questions and determines whether the answer is right
for i in range (0, 4):
print("Your current score is: %d" % score)
answer = input("%s" % questions[i])
if(answer == "keys[i]"):
    print("You are right! You get one point")
    score = score + 1
else :
    print("Wrong! You lose one point!")
    score = score - 1

#end of the test and writing the score to the file
print("Congratulations! You finished the test. Your score is %d" % score)
file.write(bytes("%d" % score, "UTF-8"))

file.close()

推荐答案

如果你想持久化数据,使用 dict 和 pickling 将是一个好方法,你可以使用单个文件并使用名称查找用户,就像您自己的方法一样,但如果两个用户具有相同的名称,则会出现问题,因此您可能需要考虑如何让每个用户选择一个唯一标识符:

If you want to persist the data, using a dict and pickling would be a good approach, you can use a single file and use the name to lookup the user, like your own approach though you will have problems if two users have the same name so you might want to think of how you would make each user choose a uniqe identifier:

questions = ["1. You won't find Jerry at home right now. He ______________ (study) in the library.",
             "2. Samantha ______________   (do) her homework at the moment.",
             "3. We______________  (play) Monopoly a lot.",
             "4. Ouch! I ______________ (cut, just) my finger!",
             "5. Sam (arrive) ______________in San Diego a week ago."]
keys = ["is studying",
        "is doing",
        "play",
        "have just cut",
        "arrived"]

import pickle


def ask(scores):
    print("Type in the verb given in brackets in the correct form.\n")
    # this loop asks questions and determines whether the answer is right
    # if new user default score to 0
    score = scores.get(name, 0)

    # zip the questions and answer together
    for q, a in zip(questions, keys):
        print("Your current score is: %d" % score)
        answer = input(q)
        if answer == a:
            print("You are right! You get one point")
            score += 1
        else:
            print("Wrong! You lose one point!")
            # don't want minus scores.
            if score > 0:
                score -= 1
    # update or create user name/score pairing and dump to file
    scores[name] = score
    with open("score.pkl", "wb") as f:
        pickle.dump(scores, f)


# creates a file with the person's name
print("Hello. This is an English test.")
name = input("What is your name?")


try:
   # first run file won't exist 
    with open("score.pkl", "rb") as f:
        scores = pickle.load(f)
except IOError as e:
    print(e)
    scores = {}

ask(scores)

这篇关于试图将一个整数输入一个文件并将其作为一个检索.蟒蛇 3x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆