Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import csv
- import os
- path = 'Language Data'
- swap = False
- fileChosen = -1
- files, question, answer, value = [], [], [], []
- chances, choices = [], []
- answered = 0
- correct = 0
- def giveData():
- with open(path + "/" + files[fileChosen], 'w', encoding = 'utf-8', newline = '') as file:
- writer = csv.writer(file, delimiter=',', quotechar='|')
- for i in range(0, len(value)):
- writer.writerow([question[i], answer[i], value[i]])
- sortData()
- def sortData():
- with open(path + "/" + files[fileChosen], 'r', encoding = 'utf-8') as infile:
- reader = csv.reader(infile, delimiter=',')
- sort = sorted(reader, key = lambda row: int(row[2]), reverse = False)
- with open(path + "/" + files[fileChosen], 'w', encoding = 'utf-8', newline = '') as outfile:
- writer = csv.writer(outfile)
- for eachline in sort:
- writer.writerow(eachline)
- takeData()
- def takeData():
- del question[:]
- del answer[:]
- del value[:]
- with open(path + "/" + files[fileChosen], 'r', encoding = 'utf-8') as file:
- reader = csv.reader(file, delimiter=',', quotechar='|')
- for row in reader:
- question.append(row[0])
- answer.append(row[1])
- value.append(int(row[2]))
- def randNumber(amount):
- del choices[:]
- del chances[:]
- for i in range(0, amount):
- choices.append(i)
- chance = 1000
- while(len(chances) < amount):
- chances.append(chance)
- chance = chance - (chance / 10) - (chance / 10)
- return random.choices(choices, chances)[0]
- def Input(message, info, data):
- message = message + " >> "
- userInput = input(message)
- if(info == "File"):
- if(userInput == "/C"):
- print("/C\n/View\n/Open <fileName>\n/New <fileName>\n/Remove <fileName>")
- return 0
- elif(userInput == "/View"):
- return 1
- elif(userInput[:5] == "/New "):
- return userInput
- elif(userInput[:8] == "/Remove "):
- return userInput
- elif(userInput[:6] == "/Open "):
- return userInput
- else:
- return "NIL"
- elif(info == "New Question" or info == "New Answer"):
- if(userInput == "/C"):
- print("/C\n/Back")
- return 0
- elif(userInput == "/Back"):
- return 1
- else:
- return userInput
- elif(info == "Remove Question"):
- if(userInput == "/C"):
- print("/C\n/Back")
- return -1
- elif(userInput == "/Back"):
- return -2
- elif(userInput.isdigit()):
- return int(userInput)
- else:
- return -3
- elif(info == "Action"):
- if(userInput == "/C"):
- print("/C\n/Back\n/Run\n/New\n/Remove\n/Remove All\n/View")
- return 0
- elif(userInput == "/Back"):
- return 1
- elif(userInput == "/Run"):
- return 2
- elif(userInput == "/New"):
- return 3
- elif(userInput == "/Remove"):
- return 4
- elif(userInput == "/Empty"):
- return 5
- elif(userInput == "/View"):
- return 6
- else:
- return 7
- elif(info == "Question"):
- if(userInput == "/C"):
- print("/C\n/Back\n/Swap\n/Reset")
- return 0
- elif(userInput == "/Back"):
- return 1
- elif(userInput == "/Swap"):
- return 2
- elif(userInput == "/Reset"):
- return 3
- else:
- return userInput
- def FilePathCheck():
- del files[:]
- if not os.path.exists(path):
- os.makedirs(path)
- for filename in os.listdir(path):
- files.append(filename)
- FilePathCheck()
- print("Input /C to display commands")
- while(True):
- userInput = Input("File", "File", 0)
- if(userInput == 1):#View Files
- for i in range(0, len(files)):
- print(files[i][:-4])
- if(len(files) == 0):
- print("None")
- elif(userInput != 0):
- if(userInput[:8] == "/Remove "):#Remove File
- userInput = userInput[8:] + ".csv"
- for i in range(0, len(files)):
- if(userInput == files[i]):
- print("Done")
- os.remove(path + "/" + files[i])
- FilePathCheck()
- break
- elif(userInput[:6] == "/Open "):#Open File
- userInput = userInput[6:] + ".csv"
- fileChosen = -1
- for i in range(0, len(files)):
- if(userInput == files[i]):
- fileChosen = i
- takeData() if os.stat(path + "/" + files[fileChosen]).st_size != 0 else giveData()
- break
- if(fileChosen != -1):
- while(True):
- userInput = Input("Action", "Action", 0)
- if(userInput == 1):
- fileChosen = -1
- del question[:]
- del answer[:]
- del value[:]
- break
- elif(userInput == 2):
- while(True):
- if(len(value) > 0):
- num = randNumber(len(value))
- if(swap == False):
- userAnswer = Input(question[num], "Question", 0)
- else:
- userAnswer = Input(answer[num], "Question", 0)
- if(userAnswer == 1):
- break
- elif(userAnswer == 2):
- swap = (True if swap == False else False)
- elif(userAnswer == 3):
- answered = 0
- correct = 0
- elif((userAnswer == answer[num] and swap == False) or (userAnswer == question[num] and swap == True)):
- answered = answered + 1
- correct = correct + 1
- value[num] = value[num] + random.randint(1, 1)
- print(f"Correct! ({correct}/{answered})")
- giveData()
- elif(userAnswer != 0):
- answered = answered + 1
- value[num] = value[num] - random.randint(3, 3)
- print(f"Wrong! The answer is: {answer[num]} ({correct}/{answered})") if swap == False else print(f"Wrong! The answer is: {question[num]} ({correct}/{answered})")
- giveData()
- else:
- print("You need to /Create some questions")
- break
- elif(userInput == 3):
- while(True):
- while(True):
- userInput = Input("New Question", "New Question", 0)
- if(userInput == 1):
- break
- elif(userInput != 0):
- newQuestion = userInput
- break
- if(userInput == 1):
- break
- while(True):
- userInput = Input("New Answer", "New Answer", 0)
- if(userInput == 1):
- break
- elif(userInput != 0):
- newAnswer = userInput
- break
- if(userInput == 1):
- break
- question.append(newQuestion)
- answer.append(newAnswer)
- value.append(0)
- giveData()
- sortData()
- elif(userInput == 4):
- while(True):
- print("Which Question?")
- for i in range(0, len(value)):
- print(i, question[i], answer[i], value[i])
- print("")
- print(len(value), "Questions")
- userInput = Input("Question Number", "Remove Question", 0)
- if(userInput == -2):
- break
- elif(userInput != -3 and userInput != -1 and userInput > -1 and userInput < len(value)):
- del question[userInput]
- del answer[userInput]
- del value[userInput]
- giveData()
- break
- elif(userInput == 5):
- open(path + "/" + files[fileChosen], 'w').close()
- del question[:]
- del answer[:]
- del value[:]
- giveData()
- elif(userInput == 6):
- print("Questions", "Answers", "Values")
- for i in range(0, len(value)):
- print(question[i], answer[i], value[i])
- print("")
- print(len(value), "Questions")
- elif(userInput[:5] == "/New "):#New File
- userInput = userInput[5:]
- if(len(userInput) > 2):
- print("Done")
- newFile = path + "/" + userInput + ".csv"
- new = open(newFile, 'w')
- new.close()
- FilePathCheck()
- else:
- print("Longer Name")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement