Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ınput-output HELP!
#1
Hi everybody!
In the code below ı M trying to write a program, that takes an integer n as command-line argument, reads
in n − 1 distinct integers between 1 and n from standard input, and writes the missing number

This is an example commend prompt(n is given 5 by user, integers are 1 3 2 5, output is 4)
$ python3 missing . py 5
1 3 2 5
<ctrl -d >
4



n = int(sys.argv[1])
i = 1
k = []
while i < n:
    a = stdio.readInt()
    i += 1
    while not stdio.isEmpty():
        k += [a]
        if a not in range(1, n):
            b = stdio.readInt()
            while not stdio.isEmpty():
                k += [b]
print(k)
    
for i in range(1, n+1):
    if i not in k:
        print(i)
Unfortunately I got this error after I entered two numbers (I gave 5 as n). Can you guuys help me?

1
2
Traceback (most recent call last):
File "C:\Users\USER\Desktop\app\missing.py", line 15, in <module>
k += [a]
MemoryError
Reply
#2
If you have an out of range input or a duplicate input, what are you supposed to do? Print an error message? Do you keep reading inputs until you have n-1 distinct values? What do you do if you run out of inputs before you have n-1 distinct values?

The reason for your error is you made a really big list. Lets assume N is 10 and the first input is 4. You append 4 to k. 4 is in range 1, 10, so you don't read b (not the right way to handle this condition btw). The loop runs again and appends 4 to k[]. k is now [4, 4]. This continues on until k is [4,4,4,4,4,....ran out of memory]. You never read another input from stdio and because of that stdio will never be empty. You are not doing anything to empty it out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output provide the filename along with the input file processed. arjunaram 1 933 Apr-13-2023, 08:15 PM
Last Post: menator01
  serial input/output barryjo 3 2,479 Dec-27-2021, 11:57 PM
Last Post: barryjo
  How to input & output parameters from command line argument shantanu97 1 2,555 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Code giving same output no matter the input. Yort 2 2,531 Dec-20-2020, 05:59 AM
Last Post: buran
  Handling multi-input/output audio in python bor1904 4 3,573 Nov-04-2020, 08:25 AM
Last Post: CHLOVRL
  single input infinite output problem Chase91 2 1,938 Sep-23-2020, 10:01 PM
Last Post: Chase91
  My code is giving my an output of zero, no matter what value I input PiyushBanarjee 1 1,897 Jul-01-2020, 04:34 AM
Last Post: bowlofred
  Help me input the Rawdata file so that it can output DataErrorFile and ValidData.txt Halozeus 6 3,145 May-01-2020, 07:11 AM
Last Post: bowlofred
  Four text files input combinations into an output file mhyga 2 2,214 Jul-28-2019, 06:52 PM
Last Post: ThomasL
  A question about subprocess taking input from command line and returning output! Aurimas 8 5,165 May-15-2019, 04:02 PM
Last Post: Aurimas

Forum Jump:

User Panel Messages

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