Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Noels Music Game - A Solution by Colin
- ## needs a two column .csv file with artist and song name
- ## needs a hst.txt file with 5 rows of 0,n/a added (to "prime it").
- ## feel free to tinker
- ## uncomment line 98 for testing
- import random ## added for what reason
- ## manually add new items here, username and password ## what other approach could you do
- user_list = ["colin", "clive", "pragya"]
- password_list = ["fire","wind","sky"]
- # setting a flag to false so loop below runs at least once
- access_ok = False
- # answers for first run
- user_name = input("enter user_name or Q to Quit: ")
- user_pword = input("enter password: ")
- #
- while access_ok != True:
- # check for Q to quit
- if user_name =="Q" or user_name == "q": ## is there another way of doing this?
- exit() ## not pretty but stops the prog, is there a cleaner way maybe
- elif user_name in user_list: # checking if the user_name is in the user_list list
- # if it is, then get the index of the user_name pull back the password from password_list and check if matches password entered
- if user_pword == password_list[user_list.index(user_name)]:
- print ("Access Approved, welcome " + user_name)
- access_ok = True # change the flag so while loop no longer runs
- else: # bad password entered
- print("invalid Password, try again")
- print(f"you entered <{user_name}> for the username and <{user_pword}> for password")
- user_name = input("enter user_name or Q to quit: ")
- user_pword = input("enter password: ")
- else: # bad username entered
- print("invalid UserName, try again")
- print(f"you entered <{user_name}> for the username and <{user_pword}> for password")
- user_name = input("enter user_name or Q to quit: ")
- user_pword = input("enter password: ")
- ## turn above into a function
- ## add in three attempts and account is locked (how could you do that?)
- ##########################################################
- """
- ### let's get a song...
- reminder with files need to CLOSE the file after or we could corrupt the file and thats bad
- sample file
- Abba, Waterloo
- Devotchka, When this world is over
- file name in **same** directory called songs.txt
- ## find out how to put the file in any location with a full file path
- """
- songFile = open("songs.csv","r") # this opens the file and creates an object pointing to it called songFile ## what type is songFile
- songList=[] # creates an empty list
- for line in songFile: # loop each time in turn
- line = line.rstrip('\n') # drop the trailing \n - you cnat see it, comment out and see
- songList.append(line) # append to song list
- songFile.close() #mega important EXAM MARKS! ## but there is another way...
- # testing
- # print(songList)
- ## We now have a list of all the songs in this format ['Abba,Waterloo', 'Devotchka,When this world is over'] comma separated
- ## now pick one at random
- ########################################################## Game Loops start here
- score = 0 # initalise a variable to hold the running score
- game_over = False # a "flag" to contol the first while loop, when this is set to true game ends
- while game_over == False: # my outer loop game play
- song_chosen = random.choice(songList)
- # test
- # print(song_chosen) ## just a wee test to prove I have a song to play with
- """
- now, I need to split this Abba,Waterloo into a artist and song ## there is probably a better way!
- im going to call it artist and song so split on the comma
- we know each line has a comma, we can search for its index
- try print(song_chosen.index(",")) = 4 for Abba,Waterloo
- """
- artist = song_chosen[0:song_chosen.index(",")] ## do i need to do any tinkering here??
- song = song_chosen[song_chosen.index(",")+1:] ## note the shorthand on the slicing there, and why the +1, experiment
- # testing ### Remove before proper play!
- #print("Help text (to be removed)",artist + " sang " + song)
- """
- so now we need to think about the initial letters of the song
- lets loop through it and look for index of a space, the next char if it exists is the letter we want
- """
- Inital_Letters_Of_Song = "" # empty string to put the letters in
- Inital_Letters_Of_Song += song[0] # the first letter is always needed this could be written as Inital_Letters_Of_Song = Inital_Letters_Of_Song + song[0] ## what does the + sign do?
- # test
- # print(Inital_Letters_Of_Song)
- for i in range(len(song)):
- if song[i] == " ": # search for a space dont for get its a DOUBLE == to test values
- Inital_Letters_Of_Song += song[i+1] # if found take the next letter and concatenate with Inital_Letters_Of_Song, building up the initials
- else:
- #Inital_Letters_Of_Song += "*" ### i tried to add in a way of putting * for number of letters but not sure it worked properly
- pass
- ##########################################################
- print()
- print("------------")
- print(artist + " sang the following song whose initials are:- ")
- print(Inital_Letters_Of_Song.upper())
- guessed = False # a second flag, for the inner while to control guessing, when set to True answer has been guessed
- attempt = 0 # a count for the number of attempts, ## what happens if you change this to try..
- while guessed == False and attempt <2: ## note the test for equality == gets me every time
- my_guess = input("Enter your guess> ") # user input
- attempt +=1 # increment attempt, could write attempt = attempt +1
- if my_guess.upper() == song.upper(): # changes my guess to upper case and see if it matches the picked song
- guessed = True # change the flag to True
- print("Well done, that's correct ")
- if attempt == 1: # update scores
- score +=3
- else: # if the attempt isn't 1 it has to be 2nd attempt
- score += 1
- # print(attempt, my_guess, score) # line added for testing ### any idea how else i could do this?
- else:
- print("Sorry not right, you entered > ", my_guess) # output with feedback
- if attempt == 2: #building up for the end of game
- game_over = True # change that flag to True
- print ("You scored >>>", score)
- ##########################################################
- # see other file for comments
- f = open("hst.txt", "a")
- f.write(str(score) + "," + user_name + "\n")
- f.close()
- # HST output
- temp_hst_list = []
- f = open("hst.txt", "r")
- fline = f.readline()
- while fline != "":
- field = fline.split(",")
- score = int(field[0])
- name = field[1]
- name = name[0:-1]
- new_hst_list_item = [score, name]
- if name.upper() !="EXIT":
- temp_hst_list.append(new_hst_list_item)
- fline = f.readline()
- f.close()
- temp_hst_list.sort(reverse = True)
- print(" *** HIGH SCORE TABLE ***")
- for i in range(5):
- print("\t",temp_hst_list[i][0], "\t", temp_hst_list[i][1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement