[英]Python 3.X NameError
我正在使用 Python 3,这段代码是用 PyCharm 编写的。 我将概率更改为 p 以防止未声明它的冲突链接到教程文章的链接我从这里获得它的网站
import numpy as np
def coinFlip(p):
#perform the binomial distribution (returns 1 or 0)
result = np.random.binomial (1 , p)
#return flip to be added to numpy array
return result
'''Main Area'''
#probability of heads vs. tails. this can be changed
p = .5
#num of flips required. this can be changed
n = 10
#initiate array
FullResults = np.arrange(n)
#perform the desired number of flips required probability set above
for i in range(0, n):
fullResults[i] = coinFlip(p)
i+=1
#print results
print("probability is set to ", p)
print("Tails = 0, Heads = 1: ", fullResults)
#Total up heads and tails for easy user experience
print("head count: ", np.count_nonzero(fullResults == 1))
print("Tail count: ", np.count_nonzero(fullResults == 0))
import numpy as np
def coinFlip(p):
#perform the binomial distribution (returns 1 or 0)
result = np.random.binomial (1 , p)
#return flip to be added to numpy array
return result
'''Main Area'''
#probability of heads vs. tails. this can be changed
p = .5
#num of flips required. this can be changed
n = 10
#initiate array
#################FullResults = np.arrange(n)
FullResults = np.arange(n)
#perform the desired number of flips required probability set above
fullResults = np.zeros(n)
for i in range(0, n):
fullResults[i] = coinFlip(p)
i+=1
#print results
print("probability is set to ", p)
print("Tails = 0, Heads = 1: ", fullResults)
#Total up heads and tails for easy user experience
print("head count: ", np.count_nonzero(fullResults == 1))
print("Tail count: ", np.count_nonzero(fullResults == 0))
我不得不做两个小的修改 -
这是 output -
probability is set to 0.5
Tails = 0, Heads = 1: [0. 0. 0. 1. 1. 1. 0. 0. 0. 1.]
head count: 4
Tail count: 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.