Python中matplotlib的问题

2024-09-28 17:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

为了更好地使用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

Tags: toyouinputaspltltclikehow
1条回答
网友
1楼 · 发布于 2024-09-28 17:29:47

documentation中,传递给label参数的值必须是A sequence of strings providing the labels for each wedge。 因此,切片的值必须是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()

相关问题 更多 >