首页 > 解决方案 > 检查两个输入是否已填充

问题描述

我需要检查两个输入是否都已填充。我已经尝试过单个 = ,还有“= 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”。

标签: python

解决方案


您需要检查字符串中是否包含某些内容。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()

推荐阅读