Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- from random import choice
- def main():
- #Get user class
- vClass = raw_input("Enter your class: ")
- vClass = vClass.lower()
- #Get file data
- fileName = "%s %s" %(vClass, ".txt")
- fileName = fileName.lower().replace(" ", "")
- f = open(fileName, "a+")
- #Count amount of lines in file
- i = 0
- for i, l in enumerate(f, 1):
- pass
- #Get users name
- while True:
- vName = raw_input("Enter your name: ")
- if vName.isalpha() == True: #Check if entered name is alphanumeric
- vName = vName.title()
- break
- else:
- print("Please enter a valid name!\n")
- #Check for user
- count = 0
- for find in range(i):
- data = f.readline()
- if vName in data:
- count+=1
- else:
- found = False
- f.close()
- #Has played more than 3 times?
- if count >=3:
- print("You have already played 3 times!")
- return 0
- else:
- score = quiz()
- toWrite = "%s %d" %(vName, score)
- f = open(fileName, "a+")
- f.write(toWrite)
- f.close()
- f = open(fileName, "r")
- i = 0
- for i, l in enumerate(f, 1):
- pass
- f.close()
- sort(fileName, i, toWrite)
- def quiz():
- score = 0
- opList = ["-","*","+"]
- #Test loop
- for test in range(10):
- #Create question
- int1 = randint(1,20)
- int2 = randint(1,20)
- op = choice(opList)
- question = "%d %s %d" %(int1,op,int2)
- answer = int(eval(question))
- print"What is: ",question,"?"
- while True:
- try:
- uAnswer = input(" ")
- if uAnswer == answer:
- print("Correct!\n")
- score+=1
- break
- else:
- print("Incorrect!\n")
- break
- except(ValueError,SyntaxError,NameError):
- print("Only enter integers!\n")
- return(score)
- def sort(fileName, i, toWrite):
- print("How do you want to sort the data?\n")
- print("1) Alphabetically")
- print("2) By score")
- while True:
- try:
- sortDirection = input(" ")
- if sortDirection == 1:
- sortAlpha(fileName, i, toWrite)
- break
- elif sortDirection == 2:
- sortNumeric(fileName, i, toWrite)
- break
- else:
- print("Please enter a valid option!")
- except(NameError):
- print("Please enter a valid option")
- def sortAlpha(fileName, i, toWrite):
- pairs = [l.strip().split(" ") for l in open(fileName, "r")]
- pairs.sort(key = lambda name_score: str(name_score[0]))
- "\n".join(name_score[0] + " " + name_score[1] + "\n" for name_score in pairs)
- pairs = str(pairs)
- pairs = pairs.replace("[[","\n").replace("]]","]").replace("]","")
- pairs = pairs.replace(", ","").replace("[", "\n").replace("''", " ").replace("'","")
- f = open(fileName, "a+")
- f.truncate(0)
- f.write(str(pairs))
- f.close()
- def sortNumeric(fileName, i, toWrite):
- pairs = [l.strip().split(" ") for l in open(fileName, "r")]
- pairs.sort(key = lambda name_score: int(name_score[1]))
- "\n".join(name_score[0] + " " + name_score[1] for name_score in pairs)
- pairs = str(pairs)
- pairs = pairs.replace("[[","").replace("]]","").replace("]","")
- pairs = pairs.replace(", ","").replace("[", "").replace("''", " ").replace("'","")
- f = open(fileName, "a+")
- f.truncate(0)
- f.write(str(pairs)+"\r\n")
- f.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement