我正在创建音乐测验。以下是我的CSV文件。我想从文件中随机选择一首歌曲和一个艺术家-显示艺术家名称,但只显示要显示的歌曲名称中每个单词的首字母。
我想知道是否有人知道如何在Python上做到这一点。该文件名为playlist.csv
我尝试了几种不同的方法,例如下面的方法,但是它不起作用。
with open('playlist.csv') as mycsv:
print(mycsv.readline()[0])
我已经导入了CSV文件,并将艺术家和歌曲存储在CSV文件中。
Songs Artists
Maroon 5 Girls Like You Andrea Garcia
I Like It Cardi B
God's Plan Drake
No Tears Left To Cry Ariana Grande
Psycho Post Malone
The Middle Maren Morris
Better Now Post Malone
In My Feelings Drake
It's Coming Home The Lightning Seeds
One Kiss Dua Lipa
选择了随机的歌曲和歌手。
例如:
M, Andrea Garcia
G, Drake
B, Post Malone
O, Dua Lipa.
目前,我还尝试创建一个if语句,如果用户对随机生成的歌曲的猜测是正确的,他们将收到一条消息,提示“正确答案”。这是我目前尝试过的。
userName = str
password = str
userName = raw_input("What is your name?")
password = raw_input("Please enter the correct password.")
if userName == "John" or "John" or password!= "musicquiz":
print("Sorry, the username is taken and/or the password is incorrect.")
quit()
if userName!= "John" and password == "musicquiz":
print("Hello", userName, "welcome to the game!")
import random
with open('playlist.csv') as mycsv:
f=mycsv.readlines()[1:]
random_line=random.choice(f).split(',')
print(random_line[0][0],",",random_line[3])
userGuess = str
userScore = int
userScore = 0
userGuess = raw_input("What is the correct name of this song?")
if userGuess == true:
print("Well done, that was the correct answer. You have scored three points")
userScore + 3
else:
print("Sorry, your guess was incorrect. You have one more chance to guess it right")
raw_input("What is the correct name of this song?")
if userGuess != true:
print("Sorry, game over.")
quit()
if userGuess == true:
print("Well done, that was the correct answer. You scored one point.")
userScore + 1
答案 0 :(得分:0)
import random
with open('playlist.csv') as mycsv:
f=mycsv.readlines()[1:] #all except header
random_line=random.choice(f).split(',')
print(random_line[0][0],",",random_line[1])
输出
M , Andrea Garcia
playlist.csv
Songs,Artists
Maroon 5 Girls Like You,Andrea Garcia
I Like It,Cardi B
God's Plan,Drake
No Tears Left To Cry,Ariana Grande
Psycho,Post Malone
The Middle,Maren Morris
Better Now,Post Malone
In My Feelings,Drake
It's Coming Home,The Lightning Seeds
One Kiss,Dua Lipa
答案 1 :(得分:0)
我等了几个星期给您我的答案,以防止您无法完成看起来很像家庭作业的事情
首先,输入文件:
playlist.csv
Songs,Artists
Maroon 5 Girls Like You,Andrea Garcia
I Like It,Cardi B
God's Plan,Drake
No Tears Left To Cry,Ariana Grande
Psycho,Post Malone
The Middle,Maren Morris
Better Now,Post Malone
In My Feelings,Drake
It's Coming Home,The Lightning Seeds
One Kiss,Dua Lipa
和代码:
import csv
import random
with open('playlist.csv', 'rb') as input_file:
csv_source = csv.DictReader(input_file)
all_songs_and_artists = list(csv_source)
song_and_artist_chosen = random.choice(all_songs_and_artists)
song_words = song_and_artist_chosen["Songs"].split()
song_initials = "".join(item[0] for item in song_words)
initials_uppercase = song_initials.upper()
print "Welcome to the music quiz"
print "-------------------------"
print "\n"
print "Here's an artist: " + song_and_artist_chosen["Artists"]
print "and the initials of the song we are searching are: " + initials_uppercase
user_guess = raw_input("What is the name of this song? ")
if song_and_artist_chosen["Songs"].upper() == user_guess.upper():
print "Well done, that was the correct answer."
else:
print "Sorry, your guess was incorrect. You have one more chance to guess it right"
user_guess = raw_input("What is the name of this song? ")
if song_and_artist_chosen["Songs"].upper() == user_guess.upper():
print "Well done, that was the correct answer."
else:
print "Sorry, game over."