how to compare keyboard input to a string in Python?

前端 未结 1 1891
一整个雨季
一整个雨季 2021-01-23 05:35

I m trying to compare keyboard input to a string:

import sys

# read from keyboard
line = sys.stdin.readline()
if line == \"stop\":
    print \'stop detected\'
e         


        
相关标签:
1条回答
  • 2021-01-23 05:59

    sys.stdin.readline() includes the trailing newline character. Either use raw_input(), or compare line.rstrip("\n") to the string you are looking for (or even line.strip().lower()).

    0 讨论(0)
提交回复
热议问题