vendredi 4 décembre 2020

How to multiply user input and data from a CSV file (Python)

I am trying to create a program which will calculate how much an artist will make on various streaming services. I want the streams to be user input and calculated by the data given in the CSV file. I keep getting key errors and invalid indexing and I'm unsure how to fix such errorsenter image description here

import csv

filename = "paymentperstream.csv"
with open(filename, 'r') as csvfile: 
    csvreader = csv.DictReader(csvfile) #reading file
    fieldnamesVariable = "'Spotify', 'Apple Music', 'Google Play Music', 'Deezer', 'Pandora', 'Amazon Music Unlimited', 'Tidal'" #variable of the row names
    rows = [" Spotify", "Apple Music", "Google Play Music", "Deezer", "Pandora", "Amazon Music Unlimited", "Tidal"] #list of the row names (doesnt do anything)

    streams=int(input("How many streams would you like to calculate: ")) #user input
    print("The options of streaming services are " + fieldnamesVariable) #gives user the options of streaming services they can choose from
    platform=input("What platform would you like to calculate your streams on: (Case Sensitive! Please type name as it appears above) ") #user chooses service
    
    if platform == "Spotify":
        for row in csvreader:
            print("Spotify will pay you $" + row["2"] + " per stream") #if the user chooses Spotify, take data from row 2 (the row with spotify in it) of the file
            print("You would earn $" + streams*row["2"]) #multiplies "streams" by the data in row 2 of the file (DOES NOT WORK, HERE IS WHERE THE ERRORS OCCUR)
            
    elif platform == "Apple Music":
        for row in csvreader:
            print("Apple Music will pay you $" + row["3"] + " per stream") #see above
            print("You would earn $" + streams*row["3"]) 
            
    elif platform == "Google Play Music":
        for row in csvreader:
            print("Google Play Music will pay you $" + row["4"] + " per stream")
            print("You would earn $" + streams*row["4"])
            
    elif platform == "Deezer":
        for row in csvreader:
            print("Deezer will pay you $" + row["5"] + " per stream")
            print("You would earn $" + streams*row["5"])
            
    elif platform == "Pandora":
        for row in csvreader:
            print("Pandora will pay you $" + row["6"] + " per stream")
            print("You would earn $" + streams*row["6"])
            
    elif platform == "Amazon Music Unlimited":
        for row in csvreader:
            print("Amazon Music Unlimited will pay you $" + row["7"] + " per stream")
            print("You would earn $" + streams*row["7"])
            
    elif platform == "Tidal":
        for row in csvreader:
            print("Tidal will pay you $" + row["8"] + " per stream")
            print("You would earn $" + streams*row["8"])
            
    else:
        print("Invalid Input") #if they dont give the name of a streaming service

Aucun commentaire:

Enregistrer un commentaire