By Debbie via Discussions on Python.org at 16Sep2022 12:22:
Can someone help me with the code below? How do I get it to print the
first letter of the song title and the artist?
Is this your own code? Just asking.
A note for the furutre: please surround code (and error messages and
output) with triple backtick “fences” - it lets the forum preserve the
indenting and punctuation. Example:
```
your code
goes here
```
Let’s run through the code, making various comments.
import random
import time
x = 0
username = input(“What is your username?”)
password = str(input(“What is the password?”))
if password == “123”:
print(“User Authenticated”)
elif password != “123”:
print(“Password Denied”)
exit()
These do not sem related to your problem. It helps to supply the
smallest code which still shows your problem.
Note that the elif
code just be an else
. Because you’ve already
tesed the password, the else
branch already knows that
password!="123"
so you don’t need to test again:
if password == "123":
print("User Authenticated")
else:
print("Password Denied")
exit()
Next you read the songlist.txt
file. You might want to paste in the
contents of your file here also so that we can see what data you’re
processing. I invented one, but it might be wrong:
artist name|song name
So, reading the file:
read = open("songlist.txt", "r")
songs = read.readlines()
The conventional way to write this is:
with open("songlist.txt", "r") as read:
songs = read.readlines()
The thing about this is that it pomptply closes the open file after the
with
statement.
songlist = []
for i in range(len(songs)):
songlist.append(songs[i].strip('\n'))
print (songs)
Did you really intend printing sings
on every pass through the loop?
The indentation puts the print()
inside the loop.
Instead of counting i
from 0
through to the length of songs
you
can iterate songs itself directly:
songlist = []
for song in songs:
songlist.append(song.strip('\n'))
and this kind of thing (making a list
from another list
) is so
common that Python has a “list comprehension” syntax for this:
songlist = [
song.strip('\n')
for song in songs
]
Here you’re using the conventional with
form of reading a file.
Excellent.
with open('songlist.txt')as file:
text = file.readlines()[0]
This line reads the entire file (file.readlines()
) then chooses just
the first line ([0]
). You could just do this:
text = file.readline()
This code seems already to work:
artist = text.split('|')[0]
print('Artist: ' + artist)
song = text.split('|')[1]
songLetter = text.split('|')[1][0]
print('First letter: ' + songLetter)
Now things get a bit odd:
while x == 0:
I’d have put the x=0
from the top of your code right here, just above
the while
. It makes the purpose of x
more clear.
choice = random.choice(songlist)
This chooses a random song from songlist
.
artist = choice.split(',', [0, 0])
song = choice.split(',', [1, 1])
Can you tell us what you’re trying to do here? The earlier code looked
like the fields in the songlist.xt
file were separated by '|'
, but
here you seem to be splitting on ','
instead. Also, the [0,0]
and
[1,1]
arguments to .split()
seem to make no sense.
If the chosen line choice
is commas seprated, then choice.split(',')
will return a list if the fields such as:
["artist name", "song name"]
and you might want these in variables. So you would want an expression
like:
fields = choice.split(',')
artist = fields[0]
song = fields[1]
or in your flatter version:
artist = choice.split(',')[0]
song = choice.split(',')[1]
Put some print()
statements in your code to print various things as
the programme runs. These can be very helpful to see what’s going on,
and why things might be failing. For example, I might put:
print("choice =", choice)
just above the artist=
line to see what choice
contains, which helps
see what will and will not work to extract the artist from choice
.
Cheers,
Cameron Simpson cs@cskk.id.au