@FrankZhang6/

project1

Python

work downtime project

fork
loading
Files
  • main.py
  • album data.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import time
import random

def parseData(fileName):
  txt = open(fileName, 'r')
  db = {}
  lines = txt.readlines()
  lastTag = ''
  subList = set()
  for line in lines:
    line = line[:len(line)-1]
    if line[0] != '\t': #sets keys of dict
      if lastTag != '' and subList != set():
        db[lastTag] = subList
        subList = set() #applies built up list to the finished key
      lastTag = line #sets new line to be the new key
    else:
      subList.add(line[1:])
  db[lastTag] = subList #adds last key
  return db

def chooseAns(choices, rightAnswers):
  if random.randint(1,100) < random.randint(1,75):
    return random.sample(rightAnswers,1)[0]
  else:
    return random.sample(choices,1)[0]

def albumTuneQuiz():
  print("You have selected the Album Tune List Quiz")
  time.sleep(1)
  print("Answer y if the listed tune is in the given album, and y if it is not.")
  time.sleep(2)
  print("Let's begin")
  time.sleep(1)
  database = parseData('title data.txt')
  choices = set()
  score = 0
  finished = {} #dict for comparison with database (once all guessed right, will match database)
  ans = ''
  for a in database:
    choices = choices|database[a] #puts all values into a single set
  while finished != database:    
    currentAlb = random.sample(list(database), 1)[0] #chooses album to ask about
    rightAnswers = database[currentAlb] #all songs that belong in album
    ans = chooseAns(choices, rightAnswers) #chooses a random tune
    while currentAlb in finished and ans in finished[currentAlb]: #ensures that already correct guesses are not asked again
      ans = chooseAns(choices, rightAnswers)
    inpString = "Is "  + ans + " in " + currentAlb + ":  "
    guess = input(inpString)
    if guess == 'y' and ans in rightAnswers:
      score += 1
      print("Correct, your score is now", score)
      if currentAlb not in finished:
        finished[currentAlb] = {ans} #creates key in finished
      else:
        finished[currentAlb].add(ans) #adds value to existing key
    elif guess == 'n' and ans not in rightAnswers:
      score += 1
      print("Correct, your score is now", score)
    elif guess == "c":
      print(database)
      print(finished)
    else:
      score -=1
      print("Incorrect, your score is now", score)
  print("You finished")