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
#3
N is a list and is therefore mutable.

In triangles() you hand back an object in line 4 (yield N), but then in line 5 you modify that object. If the caller didn't make a copy yet, their copy of the object is changed as well.

A simple change here would be to return a copy of the list so that the caller's object can't be modified later by the function.

yield list(N)
Reply


Messages In This Thread
RE: I am getting the wrong answer, and not sure why - by bowlofred - Nov-05-2020, 04:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 5,417 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  why don't i get the answer i want CompleteNewb 12 5,651 Sep-04-2021, 03:59 PM
Last Post: CompleteNewb
  Make the answer of input int and str enderfran2006 2 2,880 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  Keeps looping even after correct answer mcesmcsc 2 2,810 Dec-12-2019, 04:27 PM
Last Post: mcesmcsc
  python gives wrong string length and wrong character thienson30 2 4,178 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  I'm getting a wrong answer don't know where the bug is 357mag 4 3,938 Jul-07-2019, 11:21 PM
Last Post: DeaD_EyE
  How to answer subprocess prompt Monty 8 20,346 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