2

I'm supposed to write a code that receives two integers (m and n) and returns all the prime numbers between those numbers (essentially a range(m,n)). The original exercise is in Portuguese, but I tried to insert a translated version at the end for more context.
My issue is that Moodle (where I'm submitting the code) runs tests to see if it's correct. In each test, it gives me various lines (each with different variations of m and n in a "m1 n1 " "m2 n2 " until "mz nz " for z lines) like this:

--- Input ---

84 91
61 84
70 71
80 83
60 74
20 88
8 89
45 63
35 82
44 67
11 29
36 61
10 24
18 63

If every test had the same number of lines, I could do a loop (assuming I knew the number of lines), but it's all different. Test 1 has 14 lines, Test 2 has 4, Test 3 has 13, etc. I have literally no idea how to do this, since there's no input I could read that means "stop taking input" – it doesn't end with a blank input (""), the last one is an m,n pair. If I keep receiving input until there's no more lines, it throws me an EOF error. I know the basics from a previous class, but my current professor hasn't even taught lists in Python yet, so I'm assuming this has to be simple. I managed to get it running using try and except but I'm posting here because I wanna know if there's a simpler/different way to get it to work. My code without the try/except looks like this:

txt=input().split()
m=int(txt[0])
n=int(txt[1])
prime=[]
for i in range(m,n+1):
  divisors=[]
  for x in range(1,i+1):
    if i%x==0:
      divisors.append(x)
  if divisors==[1,i] or divisors==[1]:
    prime.append(i)
print(str(prime).translate(str.maketrans({"[":"","]":"",",":""})))

With the try/except:

ans=[]
while True:
  try:
    txt=input().split()
    m=int(txt[0])
    n=int(txt[1])
    prime=[]
    for i in range(m,n+1):
      divisors=[]
      for x in range(1,i+1):
        if i%x==0:
          divisors.append(x)
      if divisors==[1,i] or divisors==[1]:
        prime.append(i)
    ans.append(str(prime).translate(str.maketrans({"[":"","]":"",",":""})))
  except:
    for i in range(len(ans)):
      print(ans[i])
    break

Exercise:
"In this exercise, you must write a code capable of identifying all prime numbers inside a range of numbers. Remember that a number is prime if it is divisible by two numbers: 1 and itself. For example, 11 is prime because it can be exactly divided by 1 and 11. 12 isn't prime because it can be exactly divided by various numbers besides 1 and 12 (2, 3, 4 and 6).
Input: Your code will receive as input various lines with two integer in each one. In each line, the numbers x1 and x2 will appear in crescent order, separated by an empty space.
Output: The output also has various lines. Each line corresponds to a line given in the input. In each line, there must be a list of n all prime numbers between x1 and x2 (x1 ≤ p1 < p2 < . . . < p n ≤ x2, where p i, 1 ≤ in is prime. The numbers must be separated by an empty space."

minyards
  • 23
  • 4
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – ti7 Jul 09 '22 at 18:09
  • @ti7 no because in that case the user would have to type something considered "invalid" – nothing is typed in my case. if i tell my code to receive a set number of inputs, like 4 inputs, but Moodle gives me 3 lines ("1 2 " "3 2 " and "3 9 " for example), it throws an EOF error. that's why I gave up and used try/except – minyards Jul 09 '22 at 18:17
  • hmm.. you could consider simply `""` to be "invalid" and end there! alternatively, if these are in a file, you can directly iterate by-lines `with open(path.txt) as fh: for line in fh:` finally, if these are entering via `stdin`, you can treat it like a file and iterate similarly. if there is _no_ indicator of when to stop, there is similarly no way to know when to! (though you could work around this with something ugly like a timeout in a thread?.. I believe that's well beyond the scope of what's wanted) – ti7 Jul 09 '22 at 18:24
  • @ti7 i thought about files! but there's no file to be read (at least my professor hasn't told us anything, and trust me we asked). also, would `""` work if there's no such input like that? the _only_ inputs i get are "a b ", none of them are empty. – minyards Jul 09 '22 at 18:31

1 Answers1

1

Briefly experimenting, it seems like the source data is likely coming via stdin and being piped to your program-
Due to much historical wisdom, you can treat this like a normal file and iterate by-lines!

import sys
for line in sys.stdin:
    a, b = map(int, line.split())
    print(a + b)  # test for prime range left as an exercise to user

shell tests

% echo "test" | python3 -c "print('foo' + input())"
footest
% echo "test" | python3 -c "print('foo' + input() + input())"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
EOFError: EOF when reading a line

Instead of using input(), read sys.stdin as if it were a file

% echo "test" | python3 -c "import sys; exec('for line in sys.stdin: print(line)')"
test

% cat <<EOF | python3 -c "import sys; exec('for line in sys.stdin: print(line)')"
> test1
> test2
> EOF
test1

test2

(NOTE that <<EOF is producing a heredoc and each line includes the trailing \n)

running sys.stdin to int pairs example

$ cat <<EOF | python3 ./test.py
1 2
3 4
EOF
3
7
ti7
  • 16,375
  • 6
  • 40
  • 68
  • it worked!! using sys didnt even cross my mind, thank you so much!! – minyards Jul 09 '22 at 18:57
  • spectacular and good news: you probably have a professor who has been stewing in UNIX-land for a while to lead you well if they're just assuming knowledge of `stdin` et al. – ti7 Jul 09 '22 at 18:59