Scotchmistken
Scotchmistken

Reputation: 13

How can I get the first letter from multiple words and * the remaining letters?

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.

Upvotes: 1

Views: 1988

Answers (2)

daveruinseverything
daveruinseverything

Reputation: 5167

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?

Upvotes: 1

Patrick Artner
Patrick Artner

Reputation: 51653

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***

Upvotes: 1

Related Questions