加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python条件语句

发布时间:2020-12-16 23:57:24 所属栏目:Python 来源:网络整理
导读:1.分支语句(if……else……) 2.循环(for,while,嵌套循环) #for用在已知循环次数? while用在不确定循环次数和死循环 3.控制循环(break,continue,else) #break:跳出整个循环 continue:跳出本次循环,继续下一次循环? ?else:与break配合使用,循环执行完才能执

1.分支语句(if……else……)

2.循环(for,while,嵌套循环)  #for用在已知循环次数? while用在不确定循环次数和死循环

3.控制循环(break,continue,else)  #break:跳出整个循环 continue:跳出本次循环,继续下一次循环? ?else:与break配合使用,循环执行完才能执行else,否则不执行else里面的语句

<div class="cnblogs_code">

输入一个ip地址,判断操作系统的类型import=input(=os.popen( %
start1=res.find(=res.find(=
 res.find()>0:   
    tt=int(ttl[4 tt>64  tt<=128( % tt<=64( %( %()

ip=input()
=os.popen(%=ipStr.find(=ipStr[ipStr.find():ipStr.find( ttlStr==(

<span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">截取后:<span style="color: #800000">"<span style="color: #000000">)
<span style="color: #0000ff">print<span style="color: #000000">(ttlStr)
ttlInt=int(ttlStr.split(<span style="color: #800000">"<span style="color: #800000">=<span style="color: #800000">")[1<span style="color: #000000">])
<span style="color: #0000ff">print<span style="color: #000000">(ttlInt)
<span style="color: #0000ff">if ttlInt <=64<span style="color: #000000">:
<span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">此系统Linux系统<span style="color: #800000">"<span style="color: #000000">)
<span style="color: #0000ff">else<span style="color: #000000">:
<span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">此系统Windows系统<span style="color: #800000">")

2.循环(for,嵌套循环)

2.例:字幕滚动

=input(==zimu[i:]++=1 (dis,flush=True,end=) time.sleep(1) (,end=,flush=True) i%=len(zimu)

=input(= i ===(str1[i:],str1[:i],end=,flush=+=11)

=input( 1=input( f  [,
    ( 1=input(
    ( 1 f===s[1:]+=s[-1]+s[:-1(+s,end=,flush=

= input(=input( a ==  1= line[1:]+(+line,end=1 a ==  1= line[-1]+line[:-1(+line,end=1()

2.例:获得本机所有网卡以及对应的ip地址

(.center(60,=os.popen( (%inf.count(=inf.split()[1 (%(, nic =nic.split(=nlist[0].strip()[:-1=nic.find( flag<= =nlist[8].split()[1=addr.rstrip((<span style="color: #0000ff">print(<span style="color: #800000">"<span style="color: #800000">while循环的用法:<span style="color: #800000">".center(60,<span style="color: #800000">"<span style="color: #800000">*<span style="color: #800000">"<span style="color: #000000">))
i
=<span style="color: #000000">0
<span style="color: #0000ff">print
(<span style="color: #800000">"
<span style="color: #800000">%s%16s%38s
<span style="color: #800000">"
%(<span style="color: #800000">"
<span style="color: #800000">索引
<span style="color: #800000">"
,<span style="color: #800000">"
<span style="color: #800000">网卡名称
<span style="color: #800000">"
,<span style="color: #800000">"
<span style="color: #800000">IP地址
<span style="color: #800000">"
<span style="color: #000000">))
<span style="color: #0000ff">while
i<<span style="color: #000000">len(ilist):
net
=<span style="color: #000000">ilist[i]
netList
=net.split(<span style="color: #800000">"
<span style="color: #800000">n
<span style="color: #800000">"
<span style="color: #000000">)
name
=netList[0].strip()[:-1<span style="color: #000000">]
ipLine
=netList[8<span style="color: #000000">].strip()
<span style="color: #0000ff">if
ipLine.startswith(<span style="color: #800000">"
<span style="color: #800000">IPv4
<span style="color: #800000">"
<span style="color: #000000">):
addr
=ipLine[ipLine.find(<span style="color: #800000">"
<span style="color: #800000">:
<span style="color: #800000">"
)+2<span style="color: #000000">:]
addr
=addr.rstrip(<span style="color: #800000">"
<span style="color: #800000">(首选)
<span style="color: #800000">"
<span style="color: #000000">)
<span style="color: #0000ff">else
<span style="color: #000000">:
addr
=<span style="color: #800000">"
<span style="color: #800000">网络未连接!
<span style="color: #800000">"
<span style="color: #000000">
i
+=1
<span style="color: #008000">#
<span style="color: #008000">print(i,name.ljust(10),addr.rjust(25),sep="tt")

<span style="color: #0000ff">print
(<span style="color: #800000">"
<span style="color: #800000">{0:d}t{1:40s}t{2:s}
<span style="color: #800000">"
.format(i,name,addr))

运行效果如下:

(.center(30, i range(3=float(input( score<60( ()

?continue:跳出本次循环,继续下一次循环

(.center(30, i range(0,21 i%2!= (i)

break:跳出整个循环

例:输入一个ip,判断是否合法

方法一:

(.rjust(30,=input(=ipxx.split( len(ipw)==4 i range(4 (0<=int(ipw[i]) int(ipw[i])<=255( ( ((,ipxx)

方法二:

=input(=ip.strip().split(= len(list1)==4 i i.isdigit() (int(i)>=0 int(i)<=255=1 =( ( v==1(

方法三:

#for循环做ip=input(=ip.strip().split(= i len(list1)==4 i.isdigit() (int(i)>=0 int(i)<=255=1 =( ( v==1(,ip)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读