#integers to be input
n = input('Enter \"n\" trials')
x = input('Enter \"x\" number of succeses')
p = input('Enter the probability \"p\" of success on a single trial')
#Probability Distribution function
def probDist(n, x, p):
q = (1-p)**(n-x)
numerator = math.factorial(n);
denominator = math.factorial(x)* math.factorial(n-x);
C = numerator / denominator;
answer = C*p**x*q;
return answer
# Does this have to come after I define the function? Or does this matter in Python
# Also this part just doesn't work.
dist = probDist(n, x, p);
print(dist);
Run Code Online (Sandbox Code Playgroud)
这是我运行后得到的错误,我输入了所有的值.
Traceback (most recent call last):
line 17, in <module>
dist = probDist(n, x, p);
line 9, in probDist
q = (1-p)**(n-x)
TypeError: unsupported operand type(s) for -: 'int' and 'str'
Run Code Online (Sandbox Code Playgroud)
小智 5
在Python 3.x中,input 始终返回一个字符串,而不应用eval用户输入.Python 2.x input可以eval,但这很少是你想要的.如果你想要一个整数,请使用int(input(...))和如果你想要一个浮点数(与实数不完全相同,因为它只有有限的范围!),请使用float(input).(您应该抓住ValueError处理输入不合适的情况;如果这是用于锻炼/教育,那么现在可以暂时停止错误处理.)
| 归档时间: |
|
| 查看次数: |
444 次 |
| 最近记录: |