Python Forum
Call to a print in a defined function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call to a print in a defined function
#1
def fib(n):    # write Fibonacci series up to n
    """Print a Fibonacci series up to n."""
    a = 0
    b = 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()
# Now call the function we just defined:
fib(2000)
The output for this function is:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

My question is what is the function of the last print() function in the function since it has no arguments to print?
Reply
#2
The end parameter of print defaults to '\n'. By providing a value for that parameter, the function never goes to the next line. The final print, with no arguments, is there to move the standard output to the next line.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 233 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 516 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,166 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 753 May-02-2023, 08:40 AM
Last Post: Gribouillis
  How to print variables in function? samuelbachorik 3 852 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Getting NameError for a function that is defined JonWayn 2 1,057 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,776 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  How to print the output of a defined function bshoushtarian 4 1,237 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,592 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Why does absence of print command outputs quotes in function? Mark17 2 1,342 Jan-04-2022, 07:08 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020