vendredi 1 avril 2016

Trying to input a integer into a file and retrieving it as one. Python 3x

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.

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

Aucun commentaire:

Enregistrer un commentaire