英文:
I can,t figure out how to split the string correctly
问题
I'm providing the translated code part only:
myfile = open("songs.txt", "r")
import random
with open("songs.txt") as select:
song=(random.choice(select.readlines()))
artist, song_title = song.split(" ", 1)
for x in song_title:
letter1 = x[0:1]
print(artist, letter1)
Please note that I won't provide translations for the rest of the content as requested. If you have further code-related questions or need assistance with the task, feel free to ask.
英文:
I'm trying to make only the first letter of the song titles show but the artist show in full, for example - Michael Jackson B J - for billie jean, this is my code followed by the output.
myfile = open("songs.txt", "r")
import random
with open("songs.txt") as select:
song=(random.choice(select.readlines()))
artist,song_title = song.split(" ",1)
for x in song_title:
letter1=x[0:1]
print(artist,letter1)
OUTPUT:
Eminem C
Eminem L
Eminem E
Eminem A
Eminem N
Eminem I
Eminem N
Eminem G
Eminem
Eminem O
Eminem U
Eminem T
Eminem
Eminem M
Eminem Y
Eminem
Eminem C
Eminem L
Eminem O
Eminem S
Eminem E
Eminem T
Eminem
This is the whole challenge
TASK 1
Noel is creating a music quiz game.
The game stores a list of song names and their artist (e.g. the band or solo artist name). The player needs to try and guess the song name.
The game is played as follows:
• A random song name and artist are chosen.
• The artist and the first letter of each word in the song title are displayed.
• The user has two chances to guess the name of the song.
• If the user guesses the answer correctly the first time, they score 3 points. If the user guesses the answer correctly the second time they score 1 point. The game repeats.
• The game ends when a player guesses the song name incorrectly the second time. Only authorised players are allowed to play the game.
Where appropriate, input from the user should be validated.
Design, develop, test and evaluate a system that:
- Allows a player to enter their details, which are then authenticated to ensure that they are an authorised player.
- Stores a list of song names and artists in an external file.
- Selects a song from the file, displaying the artist and the first letter of each word of the song title.
- Allows the user up to two chances to guess the name of the song, stopping the game if they guess a song incorrectly on the second chance.
- If the guess is correct, add the points to the player’s score depending on the number of guesses.
- Displays the number of points the player has when the game ends.
- Stores the name of the player and their score in an external file.
- Displays the score and player name of the top 5 winning scores from the external file.
答案1
得分: 1
替代循环song_title
,循环song_title.split()
(因为split()
没有参数将在空格处分割),例如。
song = "Eminem CLEANING OUT MY CLOSET"
artist, song_title = song.split(" ", 1)
print(artist, end=" ")
for word in song_title.split():
print(word[0], end=" ")
print()
打印输出
Eminem C O M C
英文:
Instead of looping over song_title
, loop over song_title.split()
(since split()
without arguments will split at whitespace), e.g.
song = "Eminem CLEANING OUT MY CLOSET"
artist, song_title = song.split(" ",1)
print(artist, end=" ")
for word in song_title.split():
print(word[0], end=" ")
print()
prints out
Eminem C O M C
答案2
得分: 0
尝试使用以下代码:
song_letter = ""
for item in song_title.split():
song_letter += item[0] + " "
这段代码将遍历每个单词,并将其第一个字母附加到变量中。
英文:
Try with the below code:
song_letter = ""
for item in song_title.split():
song_letter+=item[0]+" "
which will iterate over each words and get the first letter appended to the variable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论