音乐测验项目

问题描述 投票:-2回答:2

我创建一个音乐测验。下面是我的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
python csv pyscripter
2个回答
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 

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."
© www.soinside.com 2019 - 2024. All rights reserved.