python - 检查两个输入是否已填充
问题描述
我需要检查两个输入是否都已填充。我已经尝试过单个 = ,还有“= True”而不是“= str()”。
from math import *
import time
def func():
x = input("Player 1")
y = input("Player 2")
if x and y == str():
print("Okay")
time.sleep(2)
print("Works.")
func()
我希望程序打印:“Okay”,等待两秒钟,然后“Works”。
解决方案
您需要检查字符串中是否包含某些内容。str() 生成一个空字符串,因此它永远不会按照您想要的方式工作。而是使用 this from math import * import time 将字符串与布尔值进行比较
def func():
x = input("Player 1")
y = input("Player 2")
if x and y: # if you want, you can do this: if bool(x) == True and bool(y) ==
#True:
print("Okay")
time.sleep(2)
print("Works.")
func()
推荐阅读
- loopbackjs - loopbackjs 和 passportConfigurator 护照-facebook
- c# - LINQ 将对象列表分组为匿名类型
- docker - amazonlinux:2 systemctl 未能获得 D-Bus 连接
- php - 通过 api S3 存储桶私有访问
- java - 如何将包含更多详细信息的经纬度地图发送到 Firebase?
- amazon-web-services - Terraform 从另一个项目导入 ECS 任务定义
- c++ - UB 是否将本地文字字符串分配给全局范围变量?
- reactjs - Azure React Test 未为 Azure 测试结果报告创建 XML 输出
- google-apps-script - 异常:数据执行未开始,因为数据对象当前处于错误状态。数据源.updateSpec()
- python - 使用 Python 获取畅通区域