Python中的matplotlib问题

利亚姆·奥图尔

我正在研究一个小型练习程序,以更好地使用python和matplotlib模块。该程序没有实际用途,我只是想了解我哪里出错了

    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()

并抛出此错误

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

UnboundLocalError: local variable 'values' referenced before assignment
杜里科

文档中,您传递给label参数的值必须为A sequence of strings providing the labels for each wedge因此,slice的值必须为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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章