Python之分支语句

  • 分支语句1:

sentence=input("请输入一句话:")
word=digital=Chinese=0#初始化
for i in sentence:
    d=ord(i)
    if d>=48 and d<=57:
        digital+=1
    elif d>65 and d<=122:
        word+=1
    else:
        Chinese+=1       
print("一共有%d个英文字母;"%word)
print("一共有%d个数字;"%digital)
print("一共有%d个汉子;"%Chinese)

执行结果: 

Python之分支语句

  • 分支语句2: 

这是一个简单区分Linux系统和Windows系统的分支语句。 

Windows系统IP:

Python之分支语句 

Linux系统IP:

Python之分支语句

代码如下:

import os
ip=input("请输入一个IP地址:")
result=os.popen("ping %s"%ip).read()
if result.find("TTL")>0:
    t1=result.find("TTL")
    t2=result.find("\n",t1)
    r=int(result[t1+4:t2])
    print("TTL=",result[t1+4:t2]),
    if r<=64:
        print("%s是一个linux主机!"%ip)
    else:
        print("%s是一个windows主机!".format(ip))
else:
    print("目标主机可能位于防火墙后,操作系统位置无法显示")

执行结果: 

 Python之分支语句