Python 忽略键盘输入的IF语句
我一直在尝试使用键盘输入来运行一个程序,以决定是否运行该程序,而if语句忽略了输入,并且始终默认为else语句。这是我的密码Python 忽略键盘输入的IF语句,python,if-statement,stdin,Python,If Statement,Stdin,我一直在尝试使用键盘输入来运行一个程序,以决定是否运行该程序,而if语句忽略了输入,并且始终默认为else语句。这是我的密码 import random import time import sys print("Do you want to run the program? Yes/No") ans = sys.stdin.readline() time.sleep(1) if ans == "Yes" or ans == "yes": '''Body of IF statem
import random
import time
import sys
print("Do you want to run the program? Yes/No")
ans = sys.stdin.readline()
time.sleep(1)
if ans == "Yes" or ans == "yes":
'''Body of IF statement'''
else:
print("Alright, have a great day!")
readline()
在其结果中包含换行符。使用strip()
或者使用更普通的input()
功能:
ans = input("Do you want to run the program? Yes/No")
并使用lower()
intead of或
允许不区分大小写的响应(除非您确实想将YES
和YES
视为no)
readline()
在其结果中包含换行符。使用strip()
或者使用更普通的input()
功能:
ans = input("Do you want to run the program? Yes/No")
并使用lower()
intead of或
允许不区分大小写的响应(除非您确实想将YES
和YES
视为no)
为什么不使用input()
函数呢?按照@Barmar的建议使用input()
。says.stdin.readline()
是否也收集终止的新行,这可能是个问题。为什么不使用input()
函数呢?按照@Barmar的建议使用input()
。是否会说.stdin.readline()
也会收集终止的新行,这可能会有问题。
if ans.lower() == 'yes':