try to run this progrmmer (console) In python but show me error

function, python

Solution

You are trying to run a Python3 code in Python2. The Python2 `print` is a keyword and just prints what's given, whereas the Python3 `print` is a function with some additional parameters. Python3 `print` was backported to Python2 and you can made it available using `from __future__ import print_function`:

>>> from __future__ import print_function
>>> def fib(n):
...     a, b = 0, 1
...     while a < n:
...         print(a, end=' ')
...         a, b = b, a+b
...     print()
... 
>>> fib(5)
0 1 1 2 3 

In plain Python2 it would look like this:

>>> def fib(n):
...     a, b = 0, 1
...     while a < n:
...         print a, ' '
...         a, b = b, a + b
...     print
... 
>>> fib(5)
0  
1  
1  
2  
3 

Unfortunately, Python2 `print` writes a newline at the end if you don't print a dot (`print 1, 'something', '.'`). See How to print without newline or space? for ways to go around it.

Or you could just store the results and then concatenate and print them at once, e.g.:

>>> def fib(n):
...     a, b = 0, 1
...     results = []
...     while a < n:
...         result.append(a)
...         a, b = b, a + b
...     print ' '.join([str(r) for r in results])
... 
>>> fib(5)
0 1 1 2 3

Problem

I am beginner in python. I want to run this programmer in console but it's show me error what wrong in it. ``` >>> def fib(n): ... a, b = 0, 1 ... while a < n: ... print (a, end=' ') File "<stdin>", line 4 print (a, end=' ') ^ SyntaxError: invalid syntax ``` my actual program, which i want to run: ``` >>> def fib(n): ... a, b = 0, 1 ... while a < n: ... print(a, end=' ') ... a, b = b, a+b ... print() >>> fib(1000) ```

Original source

Related problems