python-3.x if语句与isinstance(或我编码)一起发出[重复]

wpx232ag  于 2023-03-20  发布在  Python
关注(0)|答案(1)|浏览(131)

此问题在此处已有答案

How can I check if string input is a number?(30个答案)
昨天关门了。
我一直在敲其他键:if语句中我输入的任何整数或字符串的一部分将转到else:

def isThisAnInteger(isInt):
    if isinstance(isInt, int):
        #return isInt
        print("Top of If: statement")
        print(isinstance(isInt, int))
    else:        
        #print("Please enter a valid number.")
        print("Bottom of else: statement")
        print(isinstance(isInt, int))

iStart = isThisAnInteger(input("Enter the start of the range: "))
iEnd = isThisAnInteger(input("Enter the end of the range: "))

我期望整数满足第一个条件,而字符串或浮点型将满足else条件

6ie5vjzr

6ie5vjzr1#

def isThisAnInteger(isInt: str):
    if isInt.isdigit():
        return int(isInt)
    else:
        print("Please enter a valid number.")

iStart = isThisAnInteger(input("Enter the start of the range: "))
iEnd = isThisAnInteger(input("Enter the end of the range: "))

相关问题