Advertisement
timber101

NoelsMusicGame

Nov 11th, 2021
877
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##  Noels Music Game - A Solution by Colin
  2. ##  needs a two column .csv file with artist and song name
  3. ##  needs a hst.txt file with 5 rows of 0,n/a added (to "prime it").
  4. ##  feel free to tinker
  5. ## uncomment line 98 for testing
  6.  
  7. import random  ## added for what reason
  8.  
  9. ## manually add new items here, username and password ## what other approach could you do
  10. user_list = ["colin", "clive", "pragya"]
  11. password_list = ["fire","wind","sky"]
  12.  
  13. # setting a flag to false so loop below runs at least once
  14. access_ok = False
  15.  
  16. #  answers for first run
  17. user_name = input("enter user_name or Q to Quit: ")
  18. user_pword = input("enter password: ")
  19.  
  20. #
  21. while access_ok != True:
  22.     # check for Q to quit
  23.     if user_name =="Q" or user_name == "q":  ## is there another way of doing this?
  24.         exit()  ## not pretty but stops the prog, is there a cleaner way maybe
  25.  
  26.     elif user_name in user_list:  # checking if the user_name is in the user_list list
  27.         # if it is, then get the index of the user_name pull back the password from password_list and check if matches password entered
  28.         if user_pword == password_list[user_list.index(user_name)]:
  29.             print ("Access Approved, welcome " + user_name)
  30.             access_ok = True # change the flag so while loop no longer runs
  31.  
  32.         else:  # bad password entered
  33.             print("invalid Password, try again")
  34.             print(f"you entered <{user_name}> for the username and <{user_pword}> for password")
  35.             user_name = input("enter user_name or Q to quit: ")
  36.             user_pword = input("enter password: ")
  37.  
  38.     else:  # bad username entered
  39.         print("invalid UserName, try again")
  40.         print(f"you entered <{user_name}> for the username and <{user_pword}> for password")
  41.         user_name = input("enter user_name or Q to quit: ")
  42.         user_pword = input("enter password: ")
  43.  
  44. ##  turn above into a function
  45. ##  add in three attempts and account is locked (how could you do that?)
  46. ##########################################################
  47.  
  48. """
  49. ###  let's get a song...
  50. reminder with files need to CLOSE the file after or we could corrupt the file and thats bad
  51. sample file
  52. Abba, Waterloo
  53. Devotchka, When this world is over
  54.  
  55. file name in **same** directory called songs.txt
  56. ##  find out how to put the file in any location with a full file path
  57.  
  58. """
  59. songFile = open("songs.csv","r") # this opens the file and creates an object pointing to it called songFile ## what type is songFile
  60. songList=[] # creates an empty list
  61. for line in songFile:  # loop each time in turn
  62.     line = line.rstrip('\n') # drop the trailing \n - you cnat see it, comment out and see
  63.     songList.append(line) # append to song list
  64.  
  65. songFile.close() #mega important EXAM MARKS!  ## but there is another way...
  66.  
  67. # testing
  68. # print(songList)
  69.  
  70. ##  We now have a list of all the songs in this format ['Abba,Waterloo', 'Devotchka,When this world is over'] comma separated
  71.  
  72. ## now pick one at random
  73.  
  74. ##########################################################  Game Loops start here
  75.  
  76. score = 0  # initalise a variable to hold the running score
  77.  
  78. game_over = False  # a "flag" to contol the first while loop, when this is set to true game ends
  79.  
  80. while game_over == False:  # my outer loop game play
  81.  
  82.     song_chosen = random.choice(songList)
  83.  
  84.     # test
  85.     # print(song_chosen)  ## just a wee test to prove I have a song to play with
  86.  
  87.     """
  88.    now, I need to split this Abba,Waterloo into a artist and song ## there is probably a better way!
  89.    im going to call it artist and song so split on the comma
  90.    we know each line has a comma, we can search for its index
  91.    try print(song_chosen.index(","))  = 4 for Abba,Waterloo
  92.    """
  93.  
  94.     artist = song_chosen[0:song_chosen.index(",")]  ## do i need to do any tinkering here??
  95.     song = song_chosen[song_chosen.index(",")+1:]   ## note the shorthand on the slicing there, and why the +1, experiment
  96.  
  97.     # testing ###  Remove before proper play!
  98.     #print("Help text (to be removed)",artist + " sang " + song)    
  99.  
  100.     """
  101.    so now we need to think about the initial letters of the song
  102.    lets loop through it and look for index of a space, the next char if it exists is the letter we want
  103.  
  104.    """
  105.  
  106.     Inital_Letters_Of_Song = "" # empty string to put the letters in
  107.     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?
  108.  
  109.     # test
  110.     # print(Inital_Letters_Of_Song)
  111.    
  112.     for i in range(len(song)):
  113.         if song[i] == " ":  # search for a space dont for get its a DOUBLE == to test values
  114.             Inital_Letters_Of_Song += song[i+1] # if found take the next letter and concatenate with Inital_Letters_Of_Song, building up the initials
  115.         else:
  116.             #Inital_Letters_Of_Song += "*"  ### i tried to add in a way of putting * for number of letters but not sure it worked properly
  117.  
  118.             pass
  119.  
  120. ##########################################################
  121.        
  122.     print()
  123.     print("------------")
  124.     print(artist + " sang the following song whose initials are:- ")
  125.     print(Inital_Letters_Of_Song.upper())
  126.    
  127.     guessed = False  # a second flag, for the inner while to control guessing, when set to True answer has been guessed
  128.     attempt = 0  # a count for the number of attempts,  ## what happens if you change this to try..
  129.  
  130.     while guessed == False and attempt <2:   ##  note the test for equality ==  gets me every time
  131.         my_guess = input("Enter your guess> ")  # user input
  132.         attempt +=1 # increment attempt, could write attempt = attempt +1
  133.  
  134.         if my_guess.upper() == song.upper():  # changes my guess to upper case and see if it matches the picked song
  135.             guessed = True  # change the flag to True
  136.             print("Well done, that's correct ")
  137.             if attempt == 1:  # update scores
  138.                 score +=3
  139.             else:  # if the attempt isn't 1 it has to be 2nd attempt
  140.                 score += 1
  141.             # print(attempt, my_guess, score) # line added for testing  ###  any idea how else i could do this?
  142.         else:
  143.             print("Sorry not right, you entered > ", my_guess)  # output with feedback
  144.             if attempt == 2:  #building up for the end of game
  145.                 game_over = True # change that flag to True
  146.  
  147. print ("You scored >>>", score)
  148.    
  149. ##########################################################
  150. #  see other file for comments  
  151.  
  152. f = open("hst.txt", "a")
  153. f.write(str(score) + "," + user_name + "\n")
  154. f.close()
  155.  
  156. # HST output
  157. temp_hst_list = []
  158. f = open("hst.txt", "r")
  159. fline = f.readline()
  160.  
  161. while fline != "":  
  162.     field = fline.split(",")  
  163.     score = int(field[0])
  164.     name = field[1]
  165.     name = name[0:-1]
  166.     new_hst_list_item = [score, name]
  167.     if name.upper() !="EXIT":
  168.         temp_hst_list.append(new_hst_list_item)
  169.     fline = f.readline()
  170. f.close()
  171.  
  172. temp_hst_list.sort(reverse = True)
  173.  
  174. print("    *** HIGH SCORE TABLE ***")
  175. for i in range(5):
  176.     print("\t",temp_hst_list[i][0], "\t", temp_hst_list[i][1])
  177.  
  178.  
Advertisement
RAW Paste Data Copied
Advertisement