2020-21 Controlled Assessment OCR computer science GCSE

Watch
Dragonball_123
Badges: 1
Rep:
?
#1
Report Thread starter 4 months ago
#1
Here is my work (credits to person who helped me) can someone please check this and write the whole thing again please thanks a lot to anyone who helps


import random # Imports the random module
def readData(): # Load Files and Log In
with open("Song Names.txt", "r") as f Opens the song file
songNames = [line.rstrip('\n') for line in f]# Puts the song file into a list
f.close()
with open("Artist Names.txt", "r") as f Opens the artists file
artistNames = [line.rstrip('\n') for line in f]# Puts the artists file into a list
f.close()
with open("Usernames.txt", "r") as f Opens the username file
usernames = [line.rstrip('\n') for line in f]# Puts the username file into a list
f.close()
with open("Passwords.txt", "r") as f Opens the username file
passwords = [line.rstrip('\n') for line in f]# Puts the username file into a list
f.close()
return songNames, artistNames, usernames, passwords # Returns the newly created list

def login(usernames, passwords):
global enterUsername
enterUsername = input("Enter username\n> ")# Prompts the user to enter their username
if enterUsername in usernames: # If the username is in usernames...
enterPassword = str(input("Enter password\n> ")) # Prompts the user to enter their password
usernamePos = usernames.index(enterUsername) # Finds the position ofthe username
if enterPassword in passwords: # If the password is in passwords...
passwordPos = passwords.index(enterPassword) # Finds the position of the entered password
if usernamePos == passwordPos: # If the password and the username is in the same position
print ("You are logged in", enterUsername) # Log in!
return True
else: # If the password is not in the same position
print("You entered the wrong password...") # Inform the user about their mistake
else: # If the password is not in passwords...
print("That password is not in the file...")# Inform the user about their mistake
else: # If the username is not in usernames
print("You're not allowed to play the game...") # Inform the user about their mistake
return False # Tell the programme that the user did not log in

def chooseSong(songNames, artistNames): # Sets up the song requirew
secretSong = random.choice(songNames) # Generates a random song
sSP = songNames.index(secretSong) # Finds the position of the secret song
secretArtist = artistNames[sSP] # Gets the correct artist from the song
return secretSong, secretArtist # Returns the song or artist

def loadGame(secretSong, secretArtist): # Loads the game
word_guessed = [] # return secretSong
print ("The artist is ", secretArtist, "and the song is", secretSong) # Print the artists nam
for letter in secretSong: # Prints the song name (censored, of course)
if letter == " ": # If the ltetter is a space...
word_guessed.append(" / ") # Print a dash
else: # Otherwise...
word_guessed.append("-") # Print a line
for letter in word_guessed:
print (letter, end = " ")
return secretSong # Returns the secret song

def playGame(secretSong, score): # Plays the game
tries = 0 # Sets the amount of tries to 0
while True:
userGuess = str(input("\nWhat is the song name?\n> ")) # Gives the user the oppertunity to guess the song name
if userGuess == secretSong: # If the user guesses correctly...
print("Correct!") # Prints that they are correct
if tries == 0: # If they got the song name correct on their first try
score = score + 3 # Add 3 points to thesongNames = [line.rstrip('\n') for line in f] score
print("You earnt 3 points! You have", score, "points in total.") # States that the user has got 3 points, and states the amount of points the user has in total
time.sleep(1) # Wait a second
return score, True # Return the score and True, to run the game again
elif tries == 1: # If they got the song name correct on their second try
score = score + 1 # Add 1 point to the score
print("You earnt 1 point! You have", score, "points in total.") # States that the user has got 1 point, and states the amount of points the user has in total
time.sleep(1) # Wait a second
return score, True # Return the score and True, to run the game again
else: # If the user guessed the song wrong
tries = tries + 1 # You add a try to the programme
if tries == 2: # while the tries are equal to 2
print ("Game over! You got", score, "points!") # Prints the game over screen, showing the points they earned in the game (if any)
time.sleep(3) # Waits three seconds
return score, False # Returns False, to display the high scores
break # Breaks from the while True loop
print("You have 1 try left.") # Prints that the user has 1 try left
else:
print("Incorrect!") # Tells the user that they were wrong
time.sleep(0.5) # Wait half a second
print("You have 1 try left.") # Prints that the user has 1 try left
def highScore(score, enterUsername):
sTF1 = (enterUsername + " scored ") # Save To File part 1
sTF2 = (str(score + " points.")) # Save To File part 2
sTF = str("\n" + sTF1 + sTF2) # Save To File
with open("Scores.txt", "r") as f: # Opens the score file
highScores = [line.rstrip('\n') for line in f]
f.close()# Writes the score to the high score file
with open("Scores.txt", "a") as f:
f.write(sTF)
f.close()
openScores = input("Would you like to see the score? Y/N\n> ") # Gives the user the option to view the scores
if openScores.lower() == ("y" or "yes"):
print (highScores)
time.sleep(5)
quit()
else:
quit()

def main():
songNames, artistNames, usernames, passwords = readData()# Reads the data from text files
score = 0 # Set the score to 0
success = login(usernames, passwords)# prompts the user to login
if success == True: # If they log in successfully
while True:
print ('''Rules of the game!
1) The name of the song is in all capitals
2) There must be no space after your entry
3) Have fun!''')
secretSong, artist = chooseSong(songNames, artistNames) # Chooses a random song
loadGame(secretSong, artist) # Creates the censored song name
score, win = playGame(secretSong, score) # Plays the game
if win == False: # If you lose the game
highScore(score, enterUsername)
break
main() #Plays the game!
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

How do you prefer to get careers advice?

I like to speak to my friends and family (18)
8.74%
I like to do my own research online using careers specific websites (125)
60.68%
I like speaking to the careers advisors at school, college or uni (30)
14.56%
I prefer to listen watch videos or listen to podcasts of people in my chosen career (29)
14.08%
Something else (let us know in the thread) (4)
1.94%

Watched Threads

View All