gpt4 book ai didi

python - 为什么 sys.stdin.readline() 的结果与我预期的不相等?

转载 作者:行者123 更新时间:2023-11-28 22:02:42 24 4
gpt4 key购买 nike

我正在尝试将键盘输入与字符串进行比较:

import sys

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

当我在键盘上输入“停止”并回车时,我希望程序打印“检测到停止”,但它总是打印“未检测到停止”。我该如何解决这个问题?

最佳答案

sys.stdin.readline() 包括结尾的换行符。使用 raw_input(),或将 line.rstrip("\n") 与您要查找的字符串(甚至 line.strip()。降低())。

关于python - 为什么 sys.stdin.readline() 的结果与我预期的不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10905423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com