How to read user input until EOF?

后端 未结 4 1484
有刺的猬
有刺的猬 2020-12-13 12:22

My current code reads user input until line-break. But I am trying to change that to a format, where the user can write input until strg+d to end his input.

I curren

4条回答
  •  不知归路
    2020-12-13 13:09

    With sys.stdin.readline() you could write like this:

    import sys
    
    while True:
        input_ = sys.stdin.readline()
        if input_ == '':
            break
        print type(input_)
        sys.stdout.write(input_)
    

    Remember, whatever your input is, it is a string.

    For raw_input or input version, write like this:

    while True:
        try:
            input_ = input("Enter:\t")
            #or
            _input = raw_input("Enter:\t")
        except EOFError:
            break
        print type(input_)
        print type(_input)
        print input_
        print _input
    

提交回复
热议问题