我无法正确地拆分字符串。

huangapple go评论62阅读模式
英文:

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:

  1. Allows a player to enter their details, which are then authenticated to ensure that they are an authorised player.
  2. Stores a list of song names and artists in an external file.
  3. Selects a song from the file, displaying the artist and the first letter of each word of the song title.
  4. 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.
  5. If the guess is correct, add the points to the player’s score depending on the number of guesses.
  6. Displays the number of points the player has when the game ends.
  7. Stores the name of the player and their score in an external file.
  8. 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.

huangapple
  • 本文由 发表于 2023年6月12日 17:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定