Liam O'Toole
Liam O'Toole

Reputation: 177

Issue with matplotlib in Python

I am working on a small practice program to get better with python and the matplotlib module. This program has no real world uses, I just wanna understand where i went wrong

    import matplotlib.pyplot as plt
def main():
    getTotal()
def getTotal():
    BTC=int(input('How much would you like to allocate to BTC as a percentage: '))
    ETH=int(input('How much would you like to allocate to ETH as a percentage: '))
    LTC=int(input('How much would you like to allocate to LTC as a percentage: '))
    values=[BTC,ETH,LTC]
    if BTC+ETH+LTC>100:
        print('That was too much, try again')
        getTotal()
        del values
    slices=[BTC,ETH,LTC]
    plt.pie(values,labels=slices)
    plt.title('Crypto Allocations')
    plt.show()
main()

and it is throwing this error

 File "C:/Users/Liam/ranodm.py", line 30, in getTotal
    plt.pie(values,labels=slices)

UnboundLocalError: local variable 'values' referenced before assignment

Upvotes: 1

Views: 143

Answers (1)

DZurico
DZurico

Reputation: 617

In the documentation the values you're passing in the label parameter have to be A sequence of strings providing the labels for each wedge. So, the value of slices has to be slices=['BTC','ETH','LTC'].

import matplotlib.pyplot as plt
def main():
    getTotal()
def getTotal():
    BTC=int(input('How much would you like to allocate to BTC as a percentage: '))
    ETH=int(input('How much would you like to allocate to ETH as a percentage: '))
    LTC=int(input('How much would you like to allocate to LTC as a percentage: '))
    if BTC+ETH+LTC>100:
        print('That was too much, try again')
        getTotal()
    else:
        values=[BTC,ETH,LTC]
        slices=['BTC','ETH','LTC'] #I assume that you want strings here
        plt.pie(values,labels=slices)
        plt.title('Crypto Allocations')
        plt.show()
main()

Upvotes: 2

Related Questions