我尝试了许多不同的方法来让这段代码工作。
有谁知道如何让它工作?
import sys
y = 1
def test():
print("Hello?")
x = (sys.stdin.readline())
if x == ("hello"):
print("Ah your back")
else:
print("Huh?")
while y == 1:
test()
我尝试了许多不同的方法来让这段代码工作。
有谁知道如何让它工作?
import sys
y = 1
def test():
print("Hello?")
x = (sys.stdin.readline())
if x == ("hello"):
print("Ah your back")
else:
print("Huh?")
while y == 1:
test()
为什么不使用input()
?当它可能是最简单的方法时......
import sys
y = 1
def test():
print("Hello?")
x = input()
if x == ("hello"):
print("Ah your back")
else:
print("Huh?")
while y == 1:
test()
它最后读取带有 a 的行,\n
因此比较失败。尝试类似:
import sys
y = 1
def test():
print("Hello?")
x = (sys.stdin.readline())
if x[:-1] == ("hello"):
print("Ah your back")
else:
print("Huh?")
while y == 1:
test()
去掉换行符。
import sys
def test():
print("Hello?")
x = sys.stdin.readline().rstrip('\n')
if x == "hello":
print("Ah your back")
else:
print("Huh?")
while True:
test()
import sys
y = 1
def test():
print("Hello?")
x = sys.stdin.readline()
if x == "hello\n": #either strip newline from x or compare it with "hello\n".
print("Ah your back")
else:
print("Huh?")
test() #your while will cause stack overflow error because of infinite loop.
这应该有效:
import sys
y = 1
def test():
print("Hello?")
x = (sys.stdin.readline())
if x == ("hello\n"):
print("Ah your back")
else:
print("Huh?")
while y == 1:
test()
您缺少一个\n
或换行符,它表示字符串中的行尾。