print("[+]welcome to python3") while True: num = input("please input a num:") if num.isnumeric() == True: x = num break else: print("[-]输入失败,必须输入为数字,请重新输入~") print("[+]i got "+x)
#输入大于0,小于100的整数或者小数
#isinstance(x,type) 判断x是什么类型的
while True : x = input("[+]请输入大于0的数:") try: if isinstance(eval(x) ,(int,float))==True and 100>eval(x) >0: print("[+]ok!") break else: print("输入的数字大于100或者小于0,重新输入!") except: print("输入包含其它字符,重新输入!") print("[+]i got "+x) print(eval(x)) print(type(x)) print(type(eval(x))) #eval可以将str的“20.1” 转换为float的 20.1
while True: try: x = input("Please enter a number: ") if isinstance(eval(x),(int, float)) == True: break except ValueError and NameError: print("Oops! That was no valid number. Try again ")
字符串的内置检测函数:
#startwith() 检测字符串是否以指定字符串开头 str1 = '孙悟空头上的箍叫什么?猴头箍' result = str1.startswith('孙猴子') print(result) #endswith() 检测字符串是否以指定字符串结尾 result = str1.endswith('金针箍') print(result) #isupper() 检测字符串内容是否都是大写 str1 = 'YOU CAN YOU UP, NO CAN NO BIBI' result = str1.isupper() print(result) #islower() 检测字符串内容是否都是小写 str1 = 'you can you up,no can no bibi' result = str1.islower() print(result) #istitle() 检测字符串是否是每个单词首字母大写 str1 = 'You Hurt My Heart Deeply' result = str1.istitle() print(result) #isalnum() 检测字符串是否由数字和字母组成(汉字当做字母处理) str1 = '1234567890abcdef' result = str1.isalnum() print(result) #isalpha() 检测字符串是否由字母组成(汉字当做字母处理) str1 = '哈哈haha' result = str1.isalpha() print(result) #isdigit() 检测是否由纯数字组成的字符串 str1 = '1234567890' result = str1.isdigit() print(result) #isnumeric() 检测是否由纯数字组成的字符串 str1 = '1234567890' result = str1.isnumeric() print(result) #isdecimal() 检测是否由纯数字组成的字符串 str1 = '1234567890' result = str1.isdecimal() print(result) #isspace() 检测字符串是否由空白字符组成 str1 = ' ' result = str1.isspace() print(result)