repl.it
@BenjaminSMITH69/

NEA programming project

Python

No description

fork
loading
Files
  • main.py
  • points.txt
  • songs.txt
  • users.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import random
import time
song_artist = []
song_name = []
user_name = []
user_score = []
high_scores = []
points = 0
lives = 0 
authorised = False

print("User Check")
print("~~~~~~~~~~")

def main(authorised):
  if authorised == True:
    questions()
  else:
    readUsers(authorised)

def readUsers(authorised):
  users = []
  passwords = []
  with open("users.txt", "r") as f:
    users = f.read().splitlines()

    
  name = input("Enter your name: ").title()
  for i in range(len(users)):
    if authorised == False:
      for j in users[i]:
        if name == users[i]:
          authorised = True
          print("Access Granted")
          break
  if authorised == False:
    print("Access denied: ")
  main(authorised)

def questions():  
  print("=============================")
  print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  print("Welcome to Ben's Music Quiz!!")
  print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  print("=============================")
  print("1. Play the Quiz")
  print("2. View Scores")
  print("3. Add Songs")

  choice = int(input("Please enter one of the numbers: "))
  if choice == 1:
      playQuiz()
  elif choice == 2:
      viewScores()
  elif choice == 3:
      addSongs()
  else:
      print("You didn't enter one of the above.")

def play():
  newUser = input("Username for Quiz: ")
  with open("songs.txt", "r") as f:
      for line in f.readlines():
          songArtist, songName = line.strip().split(',')
          song_artist.append(songArtist)
          song_name.append(songName)
  tries = 2
  points = 0
  while True:
      rand = random.randint(0, len(song_name) - 1)
      words = song_name[rand].split()
      letters = [word[0] for word in words]
      print("\n", "".join(letters), ",", song_artist[rand])
      tries = 2

      while True:

          guess = input("Guess the song name: ")
          if guess.lower() == song_name[rand].lower():
              print("CORRECT")
              if tries == 2:
                  points += 3
              else:
                  points += 1
                  tries = 2
              break
          elif tries > 0:
              print("INCORRECT")
              tries -= 1
              if tries == 1:
                  print("You have", tries, "try left.")
              else:
                  print("You have", tries, "tries left.")
          elif tries == 0:
              print("INCORRECT")
              print("The correct answer is:", song_name[rand])
              print("Game Over", username)
              print("You scored", points, "points")
              print("1. Play again")
              print("2. View Scores")
              print("3. Add Songs")
              print("4. Exit")
              if points > 10:
                  hScores = open("scores.txt", "a")
                  hScores.write(newUser)
                  hScores.write(",")
                  hScores.write("%d" % points)
                  hScores.write("\n")
                  hScores.close()
              while True:
                  choice1 = int(input("Choice: "))
                  if choice1 == 1:
                      playQuiz()
                  elif choice1 == 2:
                      viewScores()
                  elif choice1 == 3:
                      addSongs()
                  elif choice1 == 4:
                      break
                  elif choice1 >= 5:
                      print("That is not an option")
              break

      if tries <= 0:
          break
    

def viewScores():
  print("View Scores")
  print()
  with open("scores.txt", "r") as f:
      high_scores = f.read().splitlines()
      for i in range(len(high_scores)):
          print(i + 1, high_scores[i])
      print()
      time.sleep(3)
      print()
      print("1. Play again")
      print("2. View Scores")
      print("3. Add Songs")
      print("4. Exit")
      while True:
          choice1 = int(input("Choice: "))
          if choice1 == 1:
              playQuiz()
          elif choice1 == 2:
              viewScores()
          elif choice1 == 3:
              addSongs()
          elif choice1 == 4:
              break
          elif choice1 > 5:
              print("That is not an option")
  
  


def addSongs():
  print("Add Songs")
  newArtist = input("Artist name: ")
  newSong = input("Song Name: ")
  f = open("songs.txt", "a")
  f.write(newArtist + ',')
  f.write(newSong + '\n')
  f.close()
  print()
  time.sleep(2)
  print("1. Play again")
  print("2. View Scores")
  print("3. Add Songs")
  print("4. Exit")
  while True:
      choice1 = int(input("Choice: "))
      if choice1 == 1:
          playQuiz()
      elif choice1 == 2:
          viewScores()
      elif choice1 == 3:
          addSongs()
      elif choice1 == 4:
          break
      elif choice1 > 5:
          print("That is not an option")


if __name__ == "__main__":
  main(authorised)
?