Reputation: 55
I need my outputs to be in 3 decimals
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()
I keep getting syntax errors whether I try to use both the '%' character and the {0:.3f} .format(pos1) method. Anybody know where I'm going wrong?
Thanks!
Upvotes: 2
Views: 247
Reputation: 58271
You don't need ,
in print function just %
is enough for example:
print("The RMS distance from the starting point is %.3f", % totalSum)
^ remove this ,
like:
print("The RMS distance from the starting point is %.3f" % totalSum)
Upvotes: 2
Reputation: 142126
You're getting the print
and formatting confused:
print("The average distance from the starting point is a %.3f" % pos1)
You should really prefer the new style formatting though:
print("Whatever {:.3f}".format(pos1))
Or, if you really wanted:
print("Whatever", format(pos1, '.3f'))
Upvotes: 0
Reputation: 599490
You have commas between the string literal and the % sign. Remove those.
print("The average distance from the starting point is a %.3f" % pos1)
Upvotes: 0
Reputation: 4392
For string interpolation, you need to put the %
operator right behind the format string:
print ("The average distance from the starting point is a %.3f" % pos1)
it is a bit more obvious if you use the more modern format
way:
print ("The average distance from the starting point is a {:.3f}".format(pos1))
Upvotes: 1