简单的计算练习带积分源码

自己写的小程序,不当之处还请多多指教

import sys
import cPickle
import random
import os

score = 0

def IfHadScoreFile():
    if not(os.path.isfile("ScoreSave")):
        NewScoreFile = open("ScoreSave","w")
        DefineRecord = {"Sopina":1000}
        cPickle.dump(DefineRecord,NewScoreFile)

def RandNum(num1,num2):
    Num1 = random.randint(num1,num2)
    Num2 = random.randint(num1,num2)
    BackNum = (str(Num1),str(Num2))
    return BackNum

def PrtQReA(num):
    sys.stdout.write(num[0])
    sys.stdout.write("+")
    sys.stdout.write(num[1])
    sys.stdout.write("=")
    ReA = int(num[0]) + int(num[1])
    return str(ReA)

def GetAnswer():
    answer=str(raw_input())
    return answer

def CheckAnswer(InputAnswer,TrueAnswer):
    if InputAnswer == TrueAnswer:
        sys.stdout.write("Good!\n")
        return "T"
    else :
        sys.stdout.write("So Bad!You Lose\n")
        return "F"

def ScoreSaver(NewScore):
    saver = open("ScoreSave","r+")
    UserName = str(raw_input("\nWhat's your name?"))
    hadsave = cPickle.load(saver)
    hadsave[UserName] = str(NewScore)
    saver.close
    os.remove("ScoreSave")
    saver = open("ScoreSave","w")
    cPickle.dump(hadsave,saver)
    saver.close


def ScoreAdder(TorF):
    global score
    if TorF == "T":
        score += 1
    elif TorF == "F":
        ScoreSaver(score)

def AboutInChoose():
    sys.stdout.write("\nThis game is made by Lommoo\n")

def EnterGameInChoose():
    TorF = "T"
    while (TorF == "T"):
        AdditionNum = RandNum(1,10000)
        TrueAnswer = PrtQReA(AdditionNum)
        InputAnswer = GetAnswer()
        TorF = CheckAnswer(InputAnswer,TrueAnswer)
        ScoreAdder(TorF)

def ScoreInChoose():
    sys.stdout.write("Here is record:\n")
    ReadRecord = open("ScoreSave","r")
    record = cPickle.load(ReadRecord)
    for key in record.keys(): 
        print "name = %s , record = %s" %(key,record[key])

def UserChoose():
    sys.stdout.write("\nPlease enter the number that you want\n")
    blank = "        "
    sys.stdout.write(blank)
    sys.stdout.write("1.Enter Game\n")
    sys.stdout.write(blank)
    sys.stdout.write("2.Record\n")
    sys.stdout.write(blank)
    sys.stdout.write("3.About\n")
    sys.stdout.write(blank)
    sys.stdout.write("4.Quite\n")
    choose = str(raw_input())
    if choose == "1":
        EnterGameInChoose()
    elif choose == "2":
        ScoreInChoose()
    elif choose == "3":
        AboutInChoose()
    elif choose == "4":
        return "q"
    return "continue"

def main():
    sys.stdout.write("\nWelcome to my game\n")
    IfHadScoreFile()
    i = True
    while i:
        IfQuite=UserChoose()
        if IfQuite == "q":
            i = False
    
if __name__ == "__main__":
    main()


你可能感兴趣的:(python,计算,练习,lom)