1、判断字符串是否代表整数
def zhengshu(s): s=s.strip() if (s[0]=="+" or s[0]=="-")and s[1:].zhengshu(): return True if s.zhengshu(): return True return False def main(): s=input("请输入字符串:") if zhengshu(s): print("是整数") else: print("不是整数") if __name__=="__main__": main()
2、生成一个随机密码,长度在7—10个字符之间,从ASCII表的第33到126位中随机抽取。
from random import randint s=7 l=10 min=33 max=126 def randompassword(): randomlength=randint(s,l) result="" for i in range(randomlength): randomchar=chr(randint(min,max)) result=result+randomchar return result def main(): print("随机生成字符为:",randompassword()) if __name__== "__main__": main()