Python Forum
I am getting the wrong answer, and not sure why
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting the wrong answer, and not sure why
#1
This is my code:

def triangles():
    N = [1]
    while True:
        yield N
        N.insert(0,0)
        N.append(0)
        N = [N[i]+N[i+1] for i in range(len(N)-1)]

n = 0
results = []
for t in triangles():
    results.append(t)
    n = n + 1
    if n == 10:
        break

for t in results:
    print(t)
The answer I want to get:

[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1],
[1, 7, 21, 35, 35, 21, 7, 1],
[1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

The output of that code:
[0, 1, 0]
[0, 1, 1, 0]
[0, 1, 2, 1, 0]
[0, 1, 3, 3, 1, 0]
[0, 1, 4, 6, 4, 1, 0]
[0, 1, 5, 10, 10, 5, 1, 0]
[0, 1, 6, 15, 20, 15, 6, 1, 0]
[0, 1, 7, 21, 35, 35, 21, 7, 1, 0]
[0, 1, 8, 28, 56, 70, 56, 28, 8, 1, 0]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

I try to debug that code,The value of 'results' changes from [[1]] into [[0,1]] when the N.insert(0,0) is ran, why?
[Image: 2c45be315c6034a8bbdb67c1dc13495409237612.jpg]
Reply


Messages In This Thread
I am getting the wrong answer, and not sure why - by riskeay - Nov-05-2020, 01:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,625 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  why don't i get the answer i want CompleteNewb 12 3,470 Sep-04-2021, 03:59 PM
Last Post: CompleteNewb
  Make the answer of input int and str enderfran2006 2 2,026 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  Keeps looping even after correct answer mcesmcsc 2 1,934 Dec-12-2019, 04:27 PM
Last Post: mcesmcsc
  python gives wrong string length and wrong character thienson30 2 3,044 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  I'm getting a wrong answer don't know where the bug is 357mag 4 2,848 Jul-07-2019, 11:21 PM
Last Post: DeaD_EyE
  How to answer subprocess prompt Monty 8 17,446 Feb-14-2018, 09:59 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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