0

I am a beginner to python and this is one of the assignments my teacher has given me. "Given an integer input, AND a list of numbers (you need a single input then the new list input), Then print yes if the input is present in the list and then print all the index positions that it appears! If the given integer is not present, just print no"

I'm pretty sure i've gotten the code right but the tests keep failing because my output is not printing in the same line.

This is my code:

    N = int(input())
    a = [int(s) for s in input().split()]

    for N in a:
      for i in range(0,len(a)):
        if a[i]==N:
          [print("Yes",i)]

     else:
       print("no")

i'm posting an image of the tests to give more context of what the problem i'm facing is. test fails I hope this is enough context for ya'll to help me out.

  • Please revisit how to create a list. Also `print()` returns `None` irrespective of what's inside the brackets. – Joe Ferndz Mar 04 '21 at 01:02
  • the list command is not something i've made myself, our teacher said that just use that( im in highschool if that makes more sense – Ali Sadruddin Mar 04 '21 at 01:04
  • Collect all indices at which `N` appears in `a`, and store them in a list. Then, if the list of indices is not empty, print `"Yes"` followed by the indices. You could either `str.join` them, or unpack them using `*` as separate arguments to `print`. If the list of indices is empty, print `"no"`. – Paul M. Mar 04 '21 at 01:04
  • "but the tests keep failing because my output is not printing in the same line." So when you [read the documentation](https://docs.python.org/3.8/library/functions.html#print) for the `print` function, did it give you any ideas about how to make the text appear on the same line? (In particular, did you notice anything interesting about the default parameter values?) How about when you followed the official tutorial to [this point](https://docs.python.org/3.8/tutorial/introduction.html#first-steps-towards-programming)? – Karl Knechtel Mar 04 '21 at 01:05
  • @KarlKnechtel ah thank you for those resources. I'm kinda lost in this class because the teacher has the dive in the deep end kinda mentality and doesn't teach us much of the theory . I'll be reading the official documentation from now. – Ali Sadruddin Mar 04 '21 at 01:08
  • @PaulM. could you dumb that down for me haha. – Ali Sadruddin Mar 04 '21 at 01:09

0 Answers0