Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6909169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:39:21+00:00 2026-05-27T08:39:21+00:00

I have a program where I use input() to take input from STDIN. I

  • 0

I have a program where I use input() to take input from STDIN.

I use the input to read the first word from a line and use it as a dictionary key, with every subsequent word added to a list which is the value of the aformentioned key.

The input is in a file names.txt:

Victor Bertha Amy Diane Erika Clare
Wyatt Diane Bertha Amy Clare Erika
Xavier Bertha Erika Clare Diane Amy
Yancey Amy Diane Clare Bertha Erika
Zeus Bertha Diane Amy Erika Clare

Amy Zeus Victor Wyatt Yancey Xavier
Bertha Xavier Wyatt Yancey Victor Zeus
Clare Wyatt Xavier Yancey Zeus Victor
Diane Victor Zeus Yancey Xavier Wyatt
Erika Yancey Wyatt Zeus Xavier Victor

So for instance, men["Victor"] = ["Bertha","Amy","Diane","Erika","Clare"].

The code is in the file GS.py (an implementation of Gale-Shapley):

if __name__ == "__main__":

    ## Data Dictionary

    ''' Name : Preferences '''
    men = dict()
    women = dict()

    ''' List of unmatched men '''
    freeMen = list()

    ''' Name : How far down in preferences '''
    count = dict()

    ''' Name : Current Match '''
    wife = dict()
    husband = dict()

    ## Reading Input
    data = input("").split("\n")
    print(data)
    readingMen = True
    for l in data:
        line = l.split()
        print(line)
        if len(line) > 1:
            newPerson = line[0]
            newPersonPreferences = list()
            for i in range(1,len(line)):
                newPersonPreferences.append(line[i])
            if readingMen:
                print("man")
                print(newPersonPreferences)
                men[newPerson] = newPersonPreferences
                wife[newPerson] = 0
                count[newPerson] = 0
                freeMen.append(newPerson)
            else:
                print("woman")
                print(newPersonPreferences)
                women[newPerson] = newPersonPreferences
                husband[newPerson] = 0
        elif len(line) == 1:
            raise IOError(l + "\nis an invalid line.")
        else:
            readingMen = False

    ## Proposing
    while len(freeMen) != 0:
        m = freeMen[0]
        w = men[m][count[m]]
        count[m] += 1
        if husband[w] == 0:
            husband[w] = m
            wife[m] = w
            freeMen.remove(m)
        else:
            try:
                if women[w].index(husband[w], women[w].index(m)):
                    freeMen.append(husband[w])
                    wife[husband[w]] = 0
                    husband[w] = m
                    wife[m] = w
                    freeMen.remove(m)
            except ValueError:
                pass

    ## Match Printing
    print()
    for m in wife:
        print(m, wife[m])

When using IDLE on Windows, I just paste the contents of this file and hit enter, and it works.

But using Ubuntu, I do python3 GS.py < names.txt and I get this:

me@glados:~$ python3 GS.py < names.txt
['Victor Bertha Amy Diane Erika Clare']
['Victor', 'Bertha', 'Amy', 'Diane', 'Erika', 'Clare']
man
['Bertha', 'Amy', 'Diane', 'Erika', 'Clare']
Traceback (most recent call last):
  File "GS.py", line 83, in <module>
    if husband[w] == 0:
KeyError: 'Bertha'

(edited) Now when I do cat names.txt | python3 GS.py I get this:

ajg9132@glados:~$ cat names.txt | python GS.py
Traceback (most recent call last):
  File "GS.py", line 50, in <module>
    data = input("").split("\n")
  File "<string>", line 1
    Victor Bertha Amy Diane Erika Clare
                ^
SyntaxError: invalid syntax

I’ve no idea what to do – kind of ignorant regarding I/O. Any help?

Edit note: I thought the two different bash commands I gave were equivalent, but then again, I’m a total noob, so an explanation for why they’re different would help too…

To clear up ambiguity, this is for an Algo homework… (sad that I understand the algorithm but not the low-level details of the OS) and I need to have a specific input and output scheme. e.g.

spock $ java GS
Victor Bertha Amy Diane Erika Clare
Wyatt Diane Bertha Amy Clare Erika
Xavier Bertha Erika Clare Diane Amy
Yancey Amy Diane Clare Bertha Erika
Zeus Bertha Diane Amy Erika Clare

Amy Zeus Victor Wyatt Yancey Xavier
Bertha Xavier Wyatt Yancey Victor Zeus
Clare Wyatt Xavier Yancey Zeus Victor
Diane Victor Zeus Yancey Xavier Wyatt
Erika Yancey Wyatt Zeus Xavier Victor

Victor Amy
Wyatt Clare
Xavier Bertha
Yancy Erika
Zeus Diane
spock $

The only reason why I wasn’t doing this was because pasting several lines of text into PuTTY made bash try to interpret each line as a command. I can’t even.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:39:21+00:00Added an answer on May 27, 2026 at 8:39 am

    The meaning of input() has changed.

    In Python 3.2: http://docs.python.org/py3k/library/functions.html#input

    In Python 2.7.2: http://docs.python.org/library/functions.html#input

    You can see this far easier with two small testing programs. The only difference is one uses the Python 2.7 interpreter and the other uses the Python 3.2 interpreter:

    $ cat input27.py 
    #!/usr/bin/python2.7
    data = input("")
    
    for l in data.split("\n"):
        print(l)
    $ cat input32.py 
    #!/usr/bin/python3.2
    data = input("")
    
    for l in data.split("\n"):
        print(l)
    $ ./input27.py < names.txt 
    Traceback (most recent call last):
      File "./input27.py", line 2, in <module>
        data = input("")
      File "<string>", line 1
        Victor Bertha Amy Diane Erika Clare
                    ^
    SyntaxError: invalid syntax
    $ ./input32.py < names.txt 
    Victor Bertha Amy Diane Erika Clare
    $ 
    

    Note that even though the Python 3.2 version doesn’t throw errors, it also doesn’t print all the lines in names.txt as one might expect.

    I don’t think the input() method is worth using. Easier would use the new-fangled for line in file: approach instead:

    $ cat fixed_input27.py 
    #!/usr/bin/python2.7
    
    import sys
    
    for line in sys.stdin:
        print(line.split()[0])
    $ cat fixed_input32.py 
    #!/usr/bin/python3.2
    
    import sys
    
    for line in sys.stdin:
        print(line.split()[0])
    $ ./fixed_input27.py < names.txt 
    Victor
    Wyatt
    Xavier
    Yancey
    Zeus
    Amy
    Bertha
    Clare
    Diane
    Erika
    $ ./fixed_input32.py < names.txt 
    Victor
    Wyatt
    Xavier
    Yancey
    Zeus
    Amy
    Bertha
    Clare
    Diane
    Erika
    $ 
    

    (I removed the one blank line from the names.txt because it caused this simple program to throw an error. It won’t actually be an issue in your full-fledged program, because you properly handle the blank line.)

    I can’t explain why input() did work under Windows, but input() feels like a horrible enough interface (who thought running the user-supplied input through eval was a good idea?!? sheesh) to just re-write it.

    Update

    Okay, I was intrigued enough to solve this all the way. I took all your debugging code back out and switched to using the for l in sys.stdin: approach:

    $ ./GS.py 
    Victor Bertha Amy Diane Erika Clare
    Wyatt Diane Bertha Amy Clare Erika
    Xavier Bertha Erika Clare Diane Amy
    Yancey Amy Diane Clare Bertha Erika
    Zeus Bertha Diane Amy Erika Clare
    
    Amy Zeus Victor Wyatt Yancey Xavier
    Bertha Xavier Wyatt Yancey Victor Zeus
    Clare Wyatt Xavier Yancey Zeus Victor
    Diane Victor Zeus Yancey Xavier Wyatt
    Erika Yancey Wyatt Zeus Xavier Victor
    
    Wyatt Clare
    Xavier Bertha
    Yancey Erika
    Zeus Diane
    Victor Amy
    $ cat GS.py 
    #!/usr/bin/python3.2
    
    if __name__ == "__main__":
        import sys
    
        ## Data Dictionary
    
        ''' Name : Preferences '''
        men = dict()
        women = dict()
    
        ''' List of unmatched men '''
        freeMen = list()
    
        ''' Name : How far down in preferences '''
        count = dict()
    
        ''' Name : Current Match '''
        wife = dict()
        husband = dict()
    
        ## Reading Input
        readingMen = True
        for l in sys.stdin:
            line = l.split()
            if len(line) > 1:
                newPerson = line[0]
                newPersonPreferences = list()
                for i in range(1,len(line)):
                    newPersonPreferences.append(line[i])
                if readingMen:
                    men[newPerson] = newPersonPreferences
                    wife[newPerson] = 0
                    count[newPerson] = 0
                    freeMen.append(newPerson)
                else:
                    women[newPerson] = newPersonPreferences
                    husband[newPerson] = 0
            elif len(line) == 1:
                raise IOError(l + "\nis an invalid line.")
            else:
                readingMen = False
    
        ## Proposing
        while len(freeMen) != 0:
            m = freeMen[0]
            w = men[m][count[m]]
            count[m] += 1
            if husband[w] == 0:
                husband[w] = m
                wife[m] = w
                freeMen.remove(m)
            else:
                try:
                    if women[w].index(husband[w], women[w].index(m)):
                        freeMen.append(husband[w])
                        wife[husband[w]] = 0
                        husband[w] = m
                        wife[m] = w
                        freeMen.remove(m)
                except ValueError:
                    pass
    
        ## Match Printing
        print()
        for m in wife:
            print(m, wife[m])
    
    $ 
    

    Note that you have to hit ^D when you’re done pasting in the input if you run it this way. (I much prefer IO redirection ./GS.py < names.txt, but if your professor will copy and paste, then make sure your prof knows to hit ^D to signal the end of input.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my program, I have been receiving an error when I use a command-line
I have several scripts that take as input a directory name, and my program
I have a program that is going to take a password as input and
I'm writing a program that needs to take input from an XBox 360 controller.
I have a program that makes use of the following method to get a
I have a program that I want to use as an autorun. I want
I have a Perl program, that needs to use packages (that I also write).
so in my program I have parts where I use try catch blocks like
I have a VS2008 C++ program where I'm wrapping a C API for use
I have made a registration program. Making use of mysql database. Can I still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.