Bottom Page

Thread Rating:
  • 4 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacing all letters in a string apart from the first letter in each word
#1
Hey,
I am coding a song guessing game and at the moment i have succeeded. But i have a problem. When the code starts and allows the user to guess the song, it shows the user what the full name of the song is.
For example:
def main():
    playerScore=0
    guessCounter = 0
    # This is where you can change the max turns and guesses
    maxTurns=999999999
    maxGuesses = 2
    while True:
        randNum = int(random.randint(0, 19))
        f=open('Songs.txt')
        linesa=f.read().splitlines()
        g=open('Artists.txt')
        linesb=g.read().splitlines()

        for turnCounter in range(maxTurns):
            randNum = int(random.randint(0, 19))
            f=open('Songs.txt')
            linesa=f.read().splitlines()
            g=open('Artists.txt')
            linesb=g.read().splitlines()

            # Pick a random song:
            print('==============================================')
            randsong =(linesa[randNum])
            randart =(linesb[randNum])
            print(randsong)
            print(randart)
            print('==============================================')
            # Prompt for a guess:
            songGuess = input("What is the song called?\n==============================================\n>> ")
This code would output a random song from a text file but it would out put it like this:

==============================================
Ruin My Life
by Zara Larsson
==============================================
What is the song called?
==============================================
but i would like it to look like this:

==============================================
R___ M_ L___
by Zara Larsson
==============================================
What is the song called?
==============================================

How do i do this exactly? Wall Wall Wall


(If you need the full code then here is the pastebin link: https://pastebin.com/6mzLnfaK)
You like this post
Quote
#2
I observe that you open files many times and never close them. Beside that, why don't you read file content into data structure and work with that?

Regarding question how to get first letter and underscores from string, one way of doing it:

>>> def obscure(song_name):
...     return ' '.join([f'{word[0]}{(len(word) - 1) * " _ "}' for word in song_name.split()])
...
>>> song = 'Ruin My Life'
>>> print(obscure(song))
R _  _  _  M _  L _  _  _

I added empty spaces around underscores for better visibility.

If list comprehension seems complicated then in plain English: give me a string which consists first letter of word and number of underscores corresponding to length of word minus one for every word in song name (this code uses f-strings, but it can be easily done with .format method)
You like this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Quote
#3
perfringo,
I am not sure how to do that as i am sorta new to python, how would i implement that into my code?
And also how would i "Read file content into data structure"?
I know how to close files but i just didn't see the need of doing it at the time as i will be constantly reading from them.

perfingo,
The code
def obscure(song_name):
    return ' '.join([f'{word[0]}{(len(word) - 1) * " _ "}' for word in song_name.split()])
didnt work...
Invalid syntax
You like this post
Quote
#4
(Jan-14-2019, 09:44 AM)Carbonix Wrote: I am not sure how to do that as i am sorta new to python, how would i implement that into my code?
And also how would i "Read file content into data structure"?
I know how to close files but i just didn't see the need of doing it at the time as i will be constantly reading from them.

You should define the function before you use it (before calling main()). Then on row 25 of your initial code modify print(randsong) to print(obscure(randsong))

There in no point of opening file several times. Especially when you don't close it (quote from unknown source: "not closing a file is a sin and you deserve all bad things which will happen to you as result of that"). Some things what can happen are described in answers to question in Quora and StackOverflow. Usually you open file, grab needed data in suitable format/structure and work with that data (not repeatedly open files without closing them). Good practice is to open files something like this: with open('spam.csv', 'r', encoding='UTF-8') as f:. In this case Python automatically closes file for you.

NB! In order to use f-strings you should have Python version 3.6 or later.
You like this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Quote
#5
Okay i got that part but im on about the actual code that you gave me
    return ' '.join([f'{word[0]}{(len(word) - 1) * " _ "}' for word in song_name.split()])
does not work, it has an invalid syntax.
and also when i run the code with this
    return ' '.join([f(word[0])((len(word) - 1) * " _ ") for word in song_name.split()])
(which hasn't got the curly brackets and is missing some speech marks)
i get:

Traceback (most recent call last):
File "N:\Yee\A\Py\SongGame - Copy.py", line 146, in <module>
unimportant()
File "N:\Yee\A\Py\SongGame - Copy.py", line 17, in unimportant
mainMenu()
File "N:\Yee\A\Py\SongGame - Copy.py", line 40, in mainMenu
main()
File "N:\Yee\A\Py\SongGame - Copy.py", line 74, in main
print(obscure(randsong))
File "N:\Yee\A\Py\SongGame - Copy.py", line 50, in obscure
return ' '.join([f(word[0])((len(word) - 1) * " _ ") for word in song_name.split()])
File "N:\Yee\A\Py\SongGame - Copy.py", line 50, in <listcomp>
return ' '.join([f(word[0])((len(word) - 1) * " _ ") for word in song_name.split()])
TypeError: '_io.TextIOWrapper' object is not callable


Any chance you could put it into example in my full code? full code is here https://pastebin.com/6mzLnfaK
(I am running python ver 3.4.1)
Quote
#6
(Jan-17-2019, 08:50 AM)Carbonix Wrote: Okay i got that part but im on about the actual code that you gave me
Python Code: (Double-click to select all)

return ' '.join([f'{word[0]}{(len(word) - 1) * " _ "}' for word in song_name.split()])
does not work, it has an invalid syntax.

Nope, it works. If you get error then you have something different

def test(song_name):
    return ' '.join([f'{word[0]}{(len(word) - 1) * " _ "}' for word in song_name.split()])

print(test("Nothing Else Matters"))
Output:
N _ _ _ _ _ _ E _ _ _ M _ _ _ _ _ _ >>>
Post your actual code and full traceback you get.


As to the other line you tried - it's really incorrect syntax.
Quote
#7
Do i have to be on a specific version of python for this to work because i literally pasted the code you put in into a new python doc and im still getting the error, do i need to import anything?
def test(song_name):
    return ' '.join([f--------->'{word[0]}{(len(word) - 1) * " _ "}'<--------- for word in song_name.split()])
 
print(test("Nothing Else Matters"))
Where i have put the arrows is where the error is happening
Quote
#8
Because of using f-strings it will work for 3.6+
in a version before 3.6 you need to replace f-string with format

def test(song_name):
    return ' '.join(['{}{}'.format(word[0], (len(word) - 1) * " _ ") for word in song_name.split()])
 
print(test("Nothing Else Matters"))
this will work also for 3.6+, but f-strings faster and more readble
Quote
#9
Well there we have it then, im on version 3.4.1
Thank you
It works now
Quote
#10
Aha, I thought you have seen this note in the perfingo's post:

(Jan-14-2019, 11:19 AM)perfringo Wrote: NB! In order to use f-strings you should have Python version 3.6 or later.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Extending my text file word count ranker and calculator Drone4four 7 296 Yesterday, 03:19 AM
Last Post: Drone4four
  Python word counter and ranker Drone4four 6 256 Jan-18-2019, 11:58 PM
Last Post: Drone4four
  a Word Inside Parenthesis in Class Decleration tahasozgen 2 134 Dec-18-2018, 05:59 AM
Last Post: Gribouillis
  Sorting lists prioritizing letters before numbers Ratherfastmofo 5 177 Nov-21-2018, 06:44 PM
Last Post: woooee
  Can python be used to search a word document for combinations of 6 digits? gkirt1053 2 164 Nov-15-2018, 06:22 PM
Last Post: gkirt1053
  Rank word frequent in a list standenman 1 156 Nov-07-2018, 09:24 PM
Last Post: Larz60+
  Replacing NaN with list of numbers gregory67 5 231 Oct-27-2018, 02:00 AM
Last Post: ichabod801
  Creating list out of the first letter of every word in a string Drone4four 5 409 Oct-05-2018, 09:42 PM
Last Post: volcano63
  How to find previous string of a string Prince_Bhatia 1 252 Sep-26-2018, 02:31 PM
Last Post: gruntfutuk
  Replacing letters on a string via location Owenix 2 221 Sep-16-2018, 10:59 AM
Last Post: gruntfutuk

Forum Jump:


Users browsing this thread: 1 Guest(s)