我用代码生成了 1 到 100 之间的 100 个随机数:
def histogram():
for x in range(100):
x = random.randint(1, 100)
print(x)
Run Code Online (Sandbox Code Playgroud)
现在我试图用直方图表示这些信息,我将 matplotlib.pyplot 导入为 plt 并尝试构建它,但我似乎遇到了问题。
我试过:
def histogram():
for x in range(100):
x = random.randint(1, 100)
return x
histogram_plot = histogram()
plt.hist(histogram_plot)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
def histogram():
for x in range(100):
x = random.randint(1, 100)
print(x)
plt.hist(x)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
如何将一个函数的结果用于另一个函数.
list = [1, 1, 1, 1]
def margin():
a = 0
b = 1.2
c = 1
for i in list:
if i == 1:
x = a + b - c
return x # x = 0.2
def calc():
for i in list:
formula = 2 + margin()
print(formula)
calc()
# 2.2 2.2 2.2 2.2
Run Code Online (Sandbox Code Playgroud)
我希望程序计算:
'''
2 + 0.2 = 2.2
2.2 + 0.2 = 2.4
2.4 + 0.2 = 2.6
2.6 + 0.2 = 2.8
giving …Run Code Online (Sandbox Code Playgroud)