Python Forum
Pattern Require Last Line Print() why?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pattern Require Last Line Print() why?
#1
for x in range(1,6):
    for y in range(5,x,-1):
        print("",end="")
    for z in range(0, x):
        print(x,end="")
    print()
Output:
1 22 333 4444 55555
Problem: This code is mine only.
i am experimenting on patterns of numbers and all i found that if remove the print() word in last line the program does not work! Can Someone tell me print() at last line is neccessary? and if neccessary but why? and explanation?
Reply
#2
What do you mean "doesn't work"? Note that calling print with no arguments will just cause the function to print its end argument, as per the docs.
Reply
#3
Ok Thanks For Telling!
Reply
#4
Hi,

The below is my interpretation of the code, I am obviously not saying that there is anything wrong with the OP's code.
I actually learnt a lot about print() tinkering with his code as well as for ndc85430 reply.

"""Sat 08/08/2020 at 09:02, my attempt at OP's code from a forum question."""

def get_pattern(num):
    """Return a list of pattern based on supplied argument."""
    pattern = []
    for i in range(0, num):
        pattern.append(num)
    return pattern

for n in range(1, 6):
    #pattern = get_pattern(n)
    #print(*pattern, sep='')
    print(*get_pattern(n), sep='')
Can someone advise if my code is ok or there is something that should be done differently?
Kind of like a simple feedback?
Reply
#5
(Aug-08-2020, 09:08 AM)Intr0spective Wrote: Hi,

The below is my interpretation of the code, I am obviously not saying that there is anything wrong with the OP's code.
I actually learnt a lot about print() tinkering with his code as well as for ndc85430 reply.

"""Sat 08/08/2020 at 09:02, my attempt at OP's code from a forum question."""

def get_pattern(num):
    """Return a list of pattern based on supplied argument."""
    pattern = []
    for i in range(0, num):
        pattern.append(num)
    return pattern

for n in range(1, 6):
    #pattern = get_pattern(n)
    #print(*pattern, sep='')
    print(*get_pattern(n), sep='')
Can someone advise if my code is ok or there is something that should be done differently?
Kind of like a simple feedback?
Well You Had A Good Approach!
Using Def Function To Print Patterns
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 335 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Why doesn't list require global keyword? johnywhy 9 646 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Print the line before the corrent line tester_V 9 1,488 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Print to a New Line when Appending File DaveG 0 1,189 Mar-30-2022, 04:14 AM
Last Post: DaveG
  If match not found print last line tester_V 2 2,845 Apr-26-2021, 05:18 AM
Last Post: tester_V
  print a line break in writelines() method leodavinci1990 1 6,358 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  Print characters in a single line rather than one at a time hhydration 1 1,989 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 5,569 Sep-30-2020, 02:53 PM
Last Post: buran
  Require Some Suggestions gouravlal 2 1,856 Jul-27-2020, 06:14 AM
Last Post: gouravlal
  print only last matched line tester_V 24 6,318 Apr-30-2020, 05:16 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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