直方图,用于python中的数字列表

问题描述 投票:-6回答:2

我的号码出了什么问题?

python jupyter-notebook introduction
2个回答
0
投票

你使用了input函数。此功能用于获取用户输入。它只能采用1或2个参数。所以换句话说你错了。

您想要存储数据的内容可能是这样的列表:numbers = [1,2,6,23,5]

您可以使用多个单独的inputcall填充该列表。或者只是按原样使用列表,如果你真的不需要用户输入。


0
投票

以下是您要组合的不同方法的两个示例,请选择其中一个

numbers = input("Enter numbers: ").split(',')
bins = int(input("Enter bin: "))

print(numbers)
print(bins)
(xenial)vash@localhost:~/python/stack_overflow$ python3.7 league.py
Enter numbers: 1, 2, 3, 4, 5
Enter bin: 4
['1', ' 2', ' 3', ' 4', ' 5']
4
numbers = [1, 2, 3, 4, 5, 6, 7]
bins = 4

print(numbers)
print(bins)
[1, 2, 3, 4, 5, 6, 7]
4

说完并触摸您的代码后,它应如下所示:

import matplotlib.pyplot as plt 
import tkinter

def histogram(numbers,bins): 
    for i in numbers:
        try: 
            numbers=list(map(float,numbers)) 
            plt.title("Histogram") 
            plt.xlabel("Value") 
            plt.ylabel("Frequency") 
            plt.show() 
            plt.close('all') 
        except: 
            print('Please Input Numbers Only') 

numbers = input("Enter numbers: ").split(',')
bins =  int(input("Enter bins: "))
histogram(numbers, bins) 
© www.soinside.com 2019 - 2024. All rights reserved.