我需要我的输出,在3位小数格式时遇到问题我小数
def main():
n = eval(input("Enter the number of random steps: "))
t = eval(input("Enter the number of trials: "))
pos = 0
totalPosition = 0
totalSum = 0
L = list()
import random
A = [-1, 1]
for x in range(t):
pos = 0
for y in range(n):
step = random.choice(A)
pos += step
totalPosition += abs(pos)
totalSum += pos**2
pos1 = totalPosition/t
totalSum /= t
totalSum = totalSum**0.5
print("The average distance from the starting point is a %.3f", % pos1)
print("The RMS distance from the starting point is %.3f", % totalSum)
main()
我不断收到语法错误,我是否尝试同时使用“%”字符和{0:.3f} .format (pos1)方法。有人知道我要去哪里错了吗?
谢谢!
不要使用'eval'为此,使用直接类型转换:'INT(输入('...'))' –