1

Trying to create a guessing game.

I've got a CSV file with 2 columns. The first contains artist names, the second contains song titles

I want to be able to display a random artist name and then the first letter of each word in the song title e.g.

Led Zeppelin - S******* t* H*****

So far I've been able to get it to select a random artist from the file and display both the artist and song title

import random
import time

filesize = 11251
offset = random.randrange(filesize)
words = []
print("Welcome to the music guessing game")
def randomsong():
    file = open("musicFile.csv","r")
    file.seek(offset)
    file.readline()
    line =file.readline()
    data = line.split(",")
    print (data[0], data[1])
    song = data[1]
    song = str(song)
    print(song)
    guesses = 2
    Score = 0
    song = song.lower()

while guesses != 0:
    yourguess = input ("Guess the name of the song")
    if yourguess == song:
        print ("Well Done!")
    else:
        print ("Incorrect, try again ")
        guesses = guesses -1
print("Game Over!!!")

randomsong()

Users should then be able to try and guess the song.

I know the code above is printing the artist and song title and the song title again, I'm just testing it to make sure it's selecting what I want.

Separate issue: the IF statement is always saying "incorrect please try again" even if I put in the correct answer.

I'm not just looking for someone to do this for me; if you could explain where I've gone wrong I'd appreciate it.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • `if yourguess == song:` in here `song` hasn't yet initialized to any value and be cause of this flow is always going to `else` block – moghya Oct 13 '18 at 10:38
  • Have a look at https://stackoverflow.com/questions/33664190/replace-all-characters-in-a-string-with-asterisks this will help you on your way. With a bit of extra logic, you can achieve your goals – kvantour Oct 13 '18 at 10:40
  • The variable `guesses` is also not initialised at the beginning of your `while` loop. I do not think that your code runs without errors. Is this the complete code? – Merlin1896 Oct 13 '18 at 10:40
  • Possible duplicate of [replace all characters in a string with asterisks](https://stackoverflow.com/questions/33664190/replace-all-characters-in-a-string-with-asterisks) – kvantour Oct 13 '18 at 10:40
  • apologise for putting the wrong tags. – Scotchmistken Oct 13 '18 at 11:29
  • I've got the guesses variable initialised before the while loop.. it's lines above it – Scotchmistken Oct 13 '18 at 11:30
  • Is the If yourguess == song not checking yourguess usr input against the song variable? ...I thought when I put song = data[1] that was putting the name of the song into the variable 'song' which can then be checked in the IF statement? – Scotchmistken Oct 13 '18 at 11:32

2 Answers2

1

To answer your OP's title:

You can use str.split() and string-slicing in concjunction with a generator expression and str.join():

def starAllButFirstCharacter(words):
    # split at whitespaces into seperate words
    w = words.split()

    # take the first character, fill up with * till length matches for
    # each word we just split. glue together with spaces and return
    return ' '.join( (word[0]+"*"*(len(word)-1) for word in w) )

print(starAllButFirstCharacter("Some song with a name"))

Output:

S*** s*** w*** a n***
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

With problems like this, start with the smallest bit first and work from the inside out.

How do I take a single word and display the first letter with asterisks for the rest?

>>> word = 'Hello'
# Use indexing to get the first letter at position 0
>>> word[0]
'H'
# Use indexing to get the remaining letters from position 1 onwards
>>> word[1:]
'ello'
# Use len to get the length of the remaining letters
>>> len(word[1:])
4
# Use the result to multiply a string containing a single asterisk
>>> '*' * len(word[1:])
'****'
# put it together
>>> word[0] + '*' * len(word[1:])
'H****'

So now you know you can get the result for a single word with word[0] + '*' * len(word[1:]), turn it into a function:

def hide_word(word):
    return word[0] + '*' * len(word[1:])

Now if you have a string of multiple words, you can use .split() to turn it into a list of individual words. Challenge for you: How do you put the results back together into a new title string?

daveruinseverything
  • 4,775
  • 28
  • 40