IF语句忽略键盘输入
IF statement ignoring keyboard input
我一直在尝试 运行 一个程序使用键盘输入来决定是否 运行 该程序和 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 statement'''
else:
print("Alright, have a great day!")
readline()
在其结果中包含换行符。使用 strip()
删除它(以及任何其他周围的空格)。
ans = sys.stdin.readline().strip()
或者使用更正常的input()
函数:
ans = input("Do you want to run the program? Yes/No")
并使用 lower()
而不是 or
以允许不区分大小写的响应(除非你真的想将 YES
和 yES
视为否)。
if ans.lower() == 'yes':
我一直在尝试 运行 一个程序使用键盘输入来决定是否 运行 该程序和 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 statement'''
else:
print("Alright, have a great day!")
readline()
在其结果中包含换行符。使用 strip()
删除它(以及任何其他周围的空格)。
ans = sys.stdin.readline().strip()
或者使用更正常的input()
函数:
ans = input("Do you want to run the program? Yes/No")
并使用 lower()
而不是 or
以允许不区分大小写的响应(除非你真的想将 YES
和 yES
视为否)。
if ans.lower() == 'yes':