0

As a beginner in Python and I stumbled upon a seemingly simple code that I can't make to work.

I'm trying to print an output of calculation with 1 decimal number, this is one of the versions of the code I tried. I also tried using format(x/s, '.1f'), but neither of those worked.

There is part of my code:

for v in range(n):
    #It will repeat the calculation the many times its defined in n input
    x = R*(v*n)
    if x/s*100<100:
        print("meridian will be", '%.1f' % x/s*100, "cm from center")
    else x/s*100>100:
        print("meridian will be - cm from center")

Using '%.1f' seems to have no effect, is the syntax wrong? I'm really not sure...

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Try replacing `% x/s*100` by `% (x/s*100)` – Sheldore Nov 03 '18 at 23:45
  • you can get plenty of inspiration from [this already existent answer](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – Antonino Nov 03 '18 at 23:46
  • Now it works perfectly, thanks. Edit: yes I searched in existing answers and tried to copy it from there, but example was a bit different. There was no need to use the () "bracket" in other examples. – Martin Dare Nov 03 '18 at 23:47
  • Maybe https://docs.python.org/3/reference/expressions.html#operator-precedence will help. – Mark Ransom Nov 04 '18 at 00:12

0 Answers0