Home > other >  How to fix the output for this function?
How to fix the output for this function?

Time:11-04

I wrote a code, with the help of a user who guided me to correct my code.

Also my code prints out 'None' in the end, and I dont want it. What should I do to fix this?

CodePudding user response:

Either do:-

def order(n:int):
  a,s =[1], [1]
  print(s)
  for i in range(0,n-1):
    s = a[i:]
    for k in range(0,len(a)):
      s.append(a[k] s[k])
    a = s
    print(s)
order(6)

Or do:-

def order(n:int):
  a,s =[1], [1]
  yield(s)
  for i in range(0,n-1):
    s = a[i:]
    for k in range(0,len(a)):
      s.append(a[k] s[k])
    a = s
    yield(s)
for i in order(6):
    print(i)

You are trying to print(print()) which prints None

CodePudding user response:

If you want to use return, then append the output s to a list, and use a for loop to get the elements -

def order(n:int):
   a,s =[1], [1]
   print(s)
   output = []
   for i in range(0,n-1):
      s = a[i:]
      for k in range(0,len(a)):
         s.append(a[k] s[k])
      a = s
      output.append(s)

   return output

for i in order(n=6):
   print(i)

Also see:

  1. Why function returns None
  2. How to return in a for loop
  • Related