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

Harman

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)
famousgarkin

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

"Could not open input file: bin/console" Error comes when try to Run the Symfony Application

From Dev

Why does python try to use apt for "run of the mill" error

From Dev

Whenever I try flutter run, it gives me the same error and I am not sure what to do

From Dev

AngularJS: How do I force Chrome's Dev Console to show me which instruction caused the error?

From Dev

Run console command with python

From Dev

When I try to run "rails s" or "rails server" command I get an error and It does not let me start the server

From Dev

Error on jest when I try to run the test

From Dev

MyFaces: how to show error messages in console log

From Dev

Upstart a Yii console gives me error

From Dev

When i am trying to run the xml file, browser show me the message Error loading stylesheet: Failure analysis XSLT stylesheet

From Dev

try except statement error python

From Dev

how to make rails console does not show me the sqlite consult

From Dev

Jquery run code and show results to console after form is submited

From Dev

How can I show "run" console in pyqt GUI (QTdesigner)?

From Dev

Getting BindingResults to show me the field associated with an error

From Dev

httpClient show me error c# winform

From Dev

"If" ternary with echo(). Parser show me an error

From Dev

"If" ternary with echo(). Parser show me an error

From Dev

httpClient show me error c# winform

From Dev

git push faid and show me the username is error

From Dev

Try and except returns error, however when run without, no error occurs?

From Dev

Python: Hello world with Flask gives me an error related to app.run(debug=True)

From Dev

Attribute error occurs when I run my GUI code in Python. Python gives me no information as to what the error is or what is causing it

From Dev

I have this error with pygame , when i try to run code from Python Crash Course (Exception has occurred: AttributeError )

From Dev

The code will not run nor will it show an error message

From Dev

Dockerfile RUN read command give me error

From Dev

How to run Odoo ORM methods in the python console?

From Dev

Run python file in child package from console

Related Related

  1. 1

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

  2. 2

    "Could not open input file: bin/console" Error comes when try to Run the Symfony Application

  3. 3

    Why does python try to use apt for "run of the mill" error

  4. 4

    Whenever I try flutter run, it gives me the same error and I am not sure what to do

  5. 5

    AngularJS: How do I force Chrome's Dev Console to show me which instruction caused the error?

  6. 6

    Run console command with python

  7. 7

    When I try to run "rails s" or "rails server" command I get an error and It does not let me start the server

  8. 8

    Error on jest when I try to run the test

  9. 9

    MyFaces: how to show error messages in console log

  10. 10

    Upstart a Yii console gives me error

  11. 11

    When i am trying to run the xml file, browser show me the message Error loading stylesheet: Failure analysis XSLT stylesheet

  12. 12

    try except statement error python

  13. 13

    how to make rails console does not show me the sqlite consult

  14. 14

    Jquery run code and show results to console after form is submited

  15. 15

    How can I show "run" console in pyqt GUI (QTdesigner)?

  16. 16

    Getting BindingResults to show me the field associated with an error

  17. 17

    httpClient show me error c# winform

  18. 18

    "If" ternary with echo(). Parser show me an error

  19. 19

    "If" ternary with echo(). Parser show me an error

  20. 20

    httpClient show me error c# winform

  21. 21

    git push faid and show me the username is error

  22. 22

    Try and except returns error, however when run without, no error occurs?

  23. 23

    Python: Hello world with Flask gives me an error related to app.run(debug=True)

  24. 24

    Attribute error occurs when I run my GUI code in Python. Python gives me no information as to what the error is or what is causing it

  25. 25

    I have this error with pygame , when i try to run code from Python Crash Course (Exception has occurred: AttributeError )

  26. 26

    The code will not run nor will it show an error message

  27. 27

    Dockerfile RUN read command give me error

  28. 28

    How to run Odoo ORM methods in the python console?

  29. 29

    Run python file in child package from console

HotTag

Archive