1

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
3
  • 1
    If you call values after del values is evaluated you get UnboundLocalError because values is no more... Commented Mar 23, 2018 at 0:48
  • @PauloScardine has the point: "values" is no more in the scope of your function because you applied del before calling the plot. Commented Mar 23, 2018 at 3:14
  • @PauloScardine was also correct, i fixed that then got to the answer i marked as correct below when that error occured Commented Dec 23, 2020 at 1:37

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.