python - Applying "%.1f" to multiple variables -
just started course , exercise learn how use round(x, n)
truncate floats
.
only problem is, didn't. found out how use %.1f
online, there way apply multiple variables instead having write on , over?
i have set below:
a = float(input("a:")) b = float(input("b:")) c = float(input("c:")) d = float(input("d:")) e = float(input("e:")) sum = + b + c + d + e average = sum/5 print ("the sum of ", "%.1f" % (a), ", ", "%.1f" % (b), ",", "%.1f" % (c), ", ", "%.1f" % (d), ", , ", "%.1f" % (e), "is ", "%.1f" % (sum), ". meanwhile, average ", "%.2f" % (average), ".")
is there way apply %.1f
without having write much?
edit: helped out. found onur güngör's have worked best i'm looking for, learned lot more needed everyone's input. appreciate it.
i guess want this.
n = 10 vars = [] = range(1, n+1): vars.append(float(input("%d: " % i))) vars_sum = sum(vars) average = vars_sum/5 str = "the sum of " var in vars[:-1]: str += "%.1f, " % var str += "and " + "%.1f" % vars[-1:][0] + " %.1f." % vars_sum str += " meanwhile, average %.2f." % average print(str)
Comments
Post a Comment