0

print (n+1) ": x1= ",x1,"f(x)= ",fx

I want it to print what the x1 is and the value of the function at x1 (fx), but I get an invalid syntax on the end of the first quotation. Can someone explain to me what my problem is.

chiktin
  • 15
  • 1
  • 4
  • If you are using Python 3, probably look at https://stackoverflow.com/questions/826948/syntax-error-on-print-with-python-3 instead. – tripleee Aug 23 '21 at 13:49

3 Answers3

1

Is this what you are trying to do ( Assuming all are integers):

print("%d: x1= %d f(x)=%d" % ((n+1),x1,fx))
tomkaith13
  • 1,717
  • 4
  • 27
  • 39
  • 1
    what if function produces floats? – Neil Apr 24 '14 at 04:37
  • feel free to use any format specifiers you want from https://docs.python.org/2/library/string.html#format-specification-mini-language if the type is only determined at run time you can use crclayton answer – tomkaith13 Apr 24 '14 at 04:49
1

You're missing a comma after n+1

print (n+1), " : x1=",x1,"f(x)=",fx 
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120
0

The problems are

  1. There should be a comma(,) before the 1st quotation.

    print (n+1), " : x1=",x1,"f(x)=",fx

  2. printing f(x) will print correct value if you have a return statement in the function
Ekta
  • 338
  • 2
  • 9
  • 26