-4

I want to write a code , that contains a while loop that only stops once the element of the list is the number 9. What is returned is a list of all of the numbers up until it reaches 9. So what it shouldn´t do : have all numbers except 9 , or all numbers that are smaller / bigger than 9 . It should just contain all numbers of a list untill the list reaches 7 . (See example below)

different operators

def hello (list):
    return[ x for x in [7, 8, 3, 2, 4, 9, 51] if x < 9]
def check_nums (list):
    return [x for x in list if x >9]

i expected the output of for example [0,2,4,9,2,3,6,8,12,14,7,9,10,8,3] to be [0, 2, 4, 9, 2, 3, 6, 8, 12, 14] .

SUSIE
  • 1
  • 2
  • 2
    Go ahead @SUSIE, what's stopping you? – Reblochon Masque Jun 11 '19 at 11:54
  • You're examples seem wrong/incoherent. Also there's no point in defining a function if you're using prefixed lists/parameters – yatu Jun 11 '19 at 11:55
  • What do you mean Reblochon Masque? – SUSIE Jun 11 '19 at 11:55
  • _A code that contains a while loop_: your examples don't have it and why do you need it? – Chris Jun 11 '19 at 11:55
  • I don´t need it , I just thought it would help the code ,or that it was necessary to achieve the output I want – SUSIE Jun 11 '19 at 11:57
  • You have asked the same question 3 times already – DSC Jun 11 '19 at 11:57
  • I mean don't ask us to do something you have not tried yourself; then, in case you are having difficulties, ask, with a description of the specific problem you faced. – Reblochon Masque Jun 11 '19 at 11:59
  • you already asked this question --> https://stackoverflow.com/questions/56538963/list-only-stops-once-the-element-of-the-list-is-the-number-7 stop spamming with the same question – gold_cy Jun 11 '19 at 12:08
  • guys please stop "attacking " me , I asked the question to get some help , because I don´t get it and trust me I´ve tried , not to get so agressive comments . – SUSIE Jun 11 '19 at 12:08
  • And the question is different – SUSIE Jun 11 '19 at 12:08
  • otherwise I wouldn´t ask , that would kind of pointless – SUSIE Jun 11 '19 at 12:09
  • no one is attacking you, this website is not meant to have the same questions asked, regardless of your intentions. please make a conscious effort to understand the answers provided in your previous question and refer to the python docs to get a better understanding, it sounds like you are not grasping certain fundamentals – gold_cy Jun 11 '19 at 12:40

1 Answers1

2

How about you make a sublist out of the initial array from 0 to the first occurrence of 9.

>>> l = [7, 8, 3, 2, 4, 9, 51, 20, 30, 9]
>>> l.index(9)
5
>>> l[:l.index(9)]
[7, 8, 3, 2, 4]
Shuvojit
  • 1,390
  • 8
  • 16