-3

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?

Bruffff
  • 53
  • 7
  • "When I tried changing print to return, the output is wrong. So how can I fix this??" Well, why do you think that happens? What is your understanding of how `return` works? When you call a function, how many times can it `return`? What happens after it `return`s? – Karl Knechtel Nov 04 '21 at 04:24
  • 1
    "Also my code prints out 'None' in the end, and I dont want it. What should I do to fix this?" Well, why do you think that happens? Which line of the code do you think is causing that to happen? Where is that value coming from? (Hint: what do you know about the return value of functions in Python? If you write `x(y())`, what gets passed to `x`? What does `y` return if it doesn't include a `return` statement?) – Karl Knechtel Nov 04 '21 at 04:26
  • 1
    If you don't know all the answers for those questions off the top of your head, then that is where [research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) comes in. If you do, then at least some of the answers to your own questions should be obvious. – Karl Knechtel Nov 04 '21 at 04:26

2 Answers2

0

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

Bibhav
  • 1,579
  • 1
  • 5
  • 18
0

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
PCM
  • 2,881
  • 2
  • 8
  • 30
  • hey thank u very much for helping me. However, is there a way where like u can put the 'for i in order(n = 6) and print(i) inside the function order(n: int)? – Bruffff Nov 04 '21 at 04:27
  • Yeah, but then you cannot use `return` statement for that. – PCM Nov 04 '21 at 04:28
  • Using `return` here is actually not needed. – PCM Nov 04 '21 at 04:30