[英]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.