sys.stdin.readline() reads without prompt, returning 'nothing in between'

前端 未结 6 2201
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 15:49

I have a function that executes the following (among other things):

userinput = stdin.readline()
betAmount = int(userinput)

Is supposed to tak

6条回答
  •  醉话见心
    2021-02-12 16:27

    If you need just one character and you don't want to keep things in the buffer, you can simply read a whole line and drop everything that isn't needed.

    Replace:

    stdin.read(1)
    

    with

    stdin.readline().strip()[:1]
    

    This will read a line, remove spaces and newlines and just keep the first character.

提交回复
热议问题