-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3827.html
642 lines (444 loc) · 33.2 KB
/
3827.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<!DOCTYPE html>
<html lang=zh>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no,email=no,adress=no">
<!-- Color theme for statusbar -->
<meta name="theme-color" content="#000000" />
<!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
<meta http-equiv="window-target" content="_top" />
<title>Python入门笔记(八) | Paneer's blog</title>
<meta name="description" content="人生苦短 我用Python">
<meta property="og:type" content="article">
<meta property="og:title" content="Python入门笔记(八)">
<meta property="og:url" content="http://paneersblog.github.io/3827.html">
<meta property="og:site_name" content="Panner&Emily’s paradise!">
<meta property="og:description" content="人生苦短 我用Python">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-07-23T08:43:13.000Z">
<meta property="article:modified_time" content="2022-07-24T14:00:17.258Z">
<meta property="article:author" content="Paneer & Emily">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<!-- Canonical links -->
<link rel="canonical" href="http://paneersblog.github.io/3827.html">
<link rel="icon" href="/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/css/style.css">
<link href="//cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.css" rel="stylesheet">
<meta name="generator" content="Hexo 6.2.0"><link rel="alternate" href="/atom.xml" title="Panner&Emily’s paradise!" type="application/atom+xml">
</head>
<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="slimContent">
<div class="navbar-header">
<div class="profile-block text-center">
<a id="avatar" href="https://github.com/paneersblog" target="_blank">
<img class="img-circle img-rotate" src="/images/IMG_4556.JPG" width="200" height="200">
</a>
<h2 id="name" class="hidden-xs hidden-sm">Paneer</h2>
<h3 id="title" class="hidden-xs hidden-sm hidden-md">一个成熟稳重的沙雕网友</h3>
<small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Shandong, China</small>
</div>
<div class="search" id="search-form-wrap">
<form class="search-form sidebar-form">
<div class="input-group">
<input type="text" class="search-form-input form-control" placeholder="搜索" />
<span class="input-group-btn">
<button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
</span>
</div>
</form>
<div class="ins-search">
<div class="ins-search-mask"></div>
<div class="ins-search-container">
<div class="ins-input-wrapper">
<input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech />
<button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="ins-section-wrapper">
<div class="ins-section-container"></div>
</div>
</div>
</div>
</div>
<button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
<ul class="nav navbar-nav main-nav ">
<li class="menu-item menu-item-home">
<a href="/.">
<i class="icon icon-home-fill"></i>
<span class="menu-title">首页</span>
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives">
<i class="icon icon-archives-fill"></i>
<span class="menu-title">归档</span>
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories">
<i class="icon icon-folder"></i>
<span class="menu-title">分类</span>
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags">
<i class="icon icon-tags"></i>
<span class="menu-title">标签</span>
</a>
</li>
<li class="menu-item menu-item-repository">
<a href="/repository">
<i class="icon icon-project"></i>
<span class="menu-title">项目</span>
</a>
</li>
<li class="menu-item menu-item-links">
<a href="/links">
<i class="icon icon-friendship"></i>
<span class="menu-title">友链</span>
</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about">
<i class="icon icon-cup-fill"></i>
<span class="menu-title">关于</span>
</a>
</li>
<li class="menu-item menu-item-播放列表">
<a href="/playlist">
<i class="icon icon-book-fill"></i>
<span class="menu-title">menu.播放列表</span>
</a>
</li>
</ul>
<ul class="social-links">
<li><a href="https://github.com/paneersblog" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
<li><a href="https://weibo.com/u/6203293798" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
<li><a href="https://twitter.com/Paneer_007" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
</ul>
</nav>
</div>
</header>
<aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
<div class="slimContent">
<div class="widget">
<h3 class="widget-title">公告</h3>
<div class="widget-body">
<div id="board">
<div class="content">
<p>欢迎交流与分享经验!</p>
</div>
</div>
</div>
</div>
<div class="widget">
<h3 class="widget-title">分类</h3>
<div class="widget-body">
<ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%94%BB%E7%95%A5/">攻略</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%B5%8B%E8%AF%95%E9%A1%B5/">测试页</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a><span class="category-list-count">16</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%8B%B1%E8%AF%AD%E5%AD%A6%E4%B9%A0/">英语学习</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%BD%AF%E4%BB%B6%E5%AD%A6%E4%B9%A0/">软件学习</a><span class="category-list-count">5</span></li></ul>
</div>
</div>
<div class="widget">
<h3 class="widget-title">标签</h3>
<div class="widget-body">
<ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/DOS/" rel="tag">DOS</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Linux/" rel="tag">Linux</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/MCNP5/" rel="tag">MCNP5</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Markdown/" rel="tag">Markdown</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Matlab/" rel="tag">Matlab</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Photo/" rel="tag">Photo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Python/" rel="tag">Python</a><span class="tag-list-count">12</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/SRIM/" rel="tag">SRIM</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Trip/" rel="tag">Trip</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/WordList-E/" rel="tag">WordList_E</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/WordList-P/" rel="tag">WordList_P</a><span class="tag-list-count">1</span></li></ul>
</div>
</div>
<div class="widget">
<h3 class="widget-title">标签云</h3>
<div class="widget-body tagcloud">
<a href="/tags/DOS/" style="font-size: 13.33px;">DOS</a> <a href="/tags/Linux/" style="font-size: 13px;">Linux</a> <a href="/tags/MCNP5/" style="font-size: 13px;">MCNP5</a> <a href="/tags/Markdown/" style="font-size: 13px;">Markdown</a> <a href="/tags/Matlab/" style="font-size: 13px;">Matlab</a> <a href="/tags/Photo/" style="font-size: 13px;">Photo</a> <a href="/tags/Python/" style="font-size: 14px;">Python</a> <a href="/tags/SRIM/" style="font-size: 13.67px;">SRIM</a> <a href="/tags/Trip/" style="font-size: 13.33px;">Trip</a> <a href="/tags/WordList-E/" style="font-size: 13px;">WordList_E</a> <a href="/tags/WordList-P/" style="font-size: 13px;">WordList_P</a>
</div>
</div>
<div class="widget">
<h3 class="widget-title">归档</h3>
<div class="widget-body">
<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/08/">八月 2022</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/07/">七月 2022</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/04/">四月 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">二月 2022</a><span class="archive-list-count">18</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">一月 2022</a><span class="archive-list-count">4</span></li></ul>
</div>
</div>
<div class="widget">
<h3 class="widget-title">最新文章</h3>
<div class="widget-body">
<ul class="recent-post-list list-unstyled no-thumbnail">
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/41781.html" class="title">Python入门笔记(11)</a>
</p>
<p class="item-date">
<time datetime="2022-08-04T12:31:21.000Z" itemprop="datePublished">2022-08-04</time>
</p>
</div>
</li>
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/34793.html" class="title">Pyhton入门笔记(十)</a>
</p>
<p class="item-date">
<time datetime="2022-08-03T07:08:20.000Z" itemprop="datePublished">2022-08-03</time>
</p>
</div>
</li>
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/21165.html" class="title">Python入门笔记(九)</a>
</p>
<p class="item-date">
<time datetime="2022-07-23T13:30:18.000Z" itemprop="datePublished">2022-07-23</time>
</p>
</div>
</li>
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/3827.html" class="title">Python入门笔记(八)</a>
</p>
<p class="item-date">
<time datetime="2022-07-23T08:43:13.000Z" itemprop="datePublished">2022-07-23</time>
</p>
</div>
</li>
<li>
<div class="item-inner">
<p class="item-category">
<a class="category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</p>
<p class="item-title">
<a href="/47766.html" class="title">Python入门笔记(七)</a>
</p>
<p class="item-date">
<time datetime="2022-07-22T06:20:09.000Z" itemprop="datePublished">2022-07-22</time>
</p>
</div>
</li>
</ul>
</div>
</div>
</div>
</aside>
<main class="main" role="main">
<div class="content">
<article id="post-Python入门笔记(八)" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
<div class="article-header">
<h1 class="article-title" itemprop="name">
Python入门笔记(八)
</h1>
<div class="article-meta">
<span class="article-date">
<i class="icon icon-calendar-check"></i>
<a href="/3827.html" class="article-date">
<time datetime="2022-07-23T08:43:13.000Z" itemprop="datePublished">2022-07-23</time>
</a>
</span>
<span class="article-category">
<i class="icon icon-folder"></i>
<a class="article-category-link" href="/categories/%E7%BC%96%E7%A8%8B%E8%AF%AD%E8%A8%80/">编程语言</a>
</span>
<span class="article-tag">
<i class="icon icon-tags"></i>
<a class="article-tag-link-link" href="/tags/Python/" rel="tag">Python</a>
</span>
<span class="post-comment"><i class="icon icon-comment"></i> <a href="/3827.html#comments" class="article-comment-link">评论</a></span>
<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 516(字)</span>
<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 2(分)</span>
</div>
</div>
<div class="article-entry marked-body" itemprop="articleBody">
<p>人生苦短 我用Python</p>
<span id="more"></span>
<h2 id="程序跳转语句"><a href="#程序跳转语句" class="headerlink" title="程序跳转语句"></a>程序跳转语句</h2><h3 id="break跳转语句"><a href="#break跳转语句" class="headerlink" title="break跳转语句"></a>break跳转语句</h3><p>只能在循环结构中使用,通常与if一起搭配使用</p>
<ul>
<li>whlie循环语法结构:</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">whlie 表达式<span class="number">1</span>:</span><br><span class="line"> 执行代码</span><br><span class="line"> <span class="keyword">if</span> 表达式<span class="number">2</span>:</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"><span class="comment"># 表达式1为False时退出循环 OR 表达式2为true时循环退出</span></span><br></pre></td></tr></table></figure>
<p>案例:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1-10内累加和大于20时的数</span></span><br><span class="line">s=<span class="number">0</span> <span class="comment"># 存储累加和</span></span><br><span class="line">i=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> i<<span class="number">11</span>:</span><br><span class="line"> s+=i</span><br><span class="line"> <span class="keyword">if</span> s><span class="number">20</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'累加和大于20的当前数为:'</span>,i)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> i+=<span class="number">1</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 模拟登录</span></span><br><span class="line">i=<span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> i<<span class="number">3</span>:</span><br><span class="line"> user_name=<span class="built_in">input</span>(<span class="string">'请输入用户名:'</span>)</span><br><span class="line"> pwd=<span class="built_in">input</span>(<span class="string">'请输入密码:'</span>)</span><br><span class="line"> <span class="keyword">if</span> user_name==<span class="string">'Paneer'</span> <span class="keyword">and</span> pwd==<span class="string">'password'</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'正在登录中...'</span>)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">if</span> i<<span class="number">2</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'账号或密码错误,您还有'</span>,<span class="number">2</span>-i,<span class="string">'次机会'</span>)</span><br><span class="line"> i+=<span class="number">1</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'连续错误三次,请稍后再试'</span>)</span><br></pre></td></tr></table></figure>
<ul>
<li>for循环语法结构</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> 循环变量 <span class="keyword">in</span> 遍历对象:</span><br><span class="line"> 执行代码</span><br><span class="line"> <span class="keyword">if</span> 表达式:</span><br><span class="line"> <span class="keyword">break</span></span><br></pre></td></tr></table></figure>
<p>案例:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line"> user_name=<span class="built_in">input</span>(<span class="string">'请输入用户名:'</span>)</span><br><span class="line"> pwd=<span class="built_in">input</span>(<span class="string">'请输入密码:'</span>)</span><br><span class="line"> <span class="keyword">if</span> user_name==<span class="string">'Paneer'</span> <span class="keyword">and</span> pwd==<span class="string">'password'</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'正在登录中...'</span>)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">if</span> i<<span class="number">2</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'账号或密码错误,您还有'</span>,<span class="number">2</span>-i,<span class="string">'次机会'</span>)</span><br><span class="line"> i+=<span class="number">1</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'连续错误三次,请稍后再试'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="continue跳转语句"><a href="#continue跳转语句" class="headerlink" title="continue跳转语句"></a>continue跳转语句</h3><p>break会跳出循环,而continue只会结束本次循环</p>
<ul>
<li>语法结构</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> 表达式<span class="number">1</span>:</span><br><span class="line"> 执行代码</span><br><span class="line"> <span class="keyword">if</span> 表达式<span class="number">2</span>:</span><br><span class="line"> <span class="keyword">continue</span></span><br></pre></td></tr></table></figure>
<p>案例:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 计算1-100内的偶数和</span></span><br><span class="line">s=<span class="number">0</span></span><br><span class="line">i=<span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> i<=<span class="number">100</span>:</span><br><span class="line"> <span class="keyword">if</span> i%<span class="number">2</span>==<span class="number">1</span>:</span><br><span class="line"> i+=<span class="number">1</span></span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> s+=i</span><br><span class="line"> i+=<span class="number">1</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'1-100内的偶数和为:'</span>,s)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 计算1-100内的偶数和</span></span><br><span class="line">s=<span class="number">0</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">101</span>):</span><br><span class="line"> <span class="keyword">if</span> i%<span class="number">2</span>==<span class="number">1</span>:</span><br><span class="line"> i+=<span class="number">1</span></span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> s+=i</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'1-100内的偶数和为:'</span>,s)</span><br></pre></td></tr></table></figure>
<h2 id="空语句pass"><a href="#空语句pass" class="headerlink" title="空语句pass"></a>空语句pass</h2><ul>
<li>不做任何事情,只起到占位符作用,使用语法结构完整,不报错</li>
<li>一般可用在 if、for、while、函数的定义、类的定义中</li>
</ul>
<hr>
<ul>
<li><strong>语法结构:</strong></li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 表达式:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"><span class="comment"># --------------------</span></span><br><span class="line"><span class="keyword">while</span> 表达式:</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"><span class="comment"># --------------------</span></span><br><span class="line"><span class="keyword">for</span> 循环变量 <span class="keyword">in</span> 遍历对象:</span><br><span class="line"> <span class="keyword">pass</span></span><br></pre></td></tr></table></figure>
</div>
<div class="article-footer">
<blockquote class="mt-2x">
<ul class="post-copyright list-unstyled">
<li class="post-copyright-link hidden-xs">
<strong>本文链接:</strong>
<a href="http://paneersblog.github.io/3827.html" title="Python入门笔记(八)" target="_blank" rel="external">http://paneersblog.github.io/3827.html</a>
</li>
<li class="post-copyright-license">
<strong>版权声明: </strong> 本博客所有文章除特别声明外,均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处!
</li>
</ul>
</blockquote>
<div class="panel panel-default panel-badger">
<div class="panel-body">
<figure class="media">
<div class="media-left">
<a href="https://github.com/paneersblog" target="_blank" class="img-burn thumb-sm visible-lg">
<img src="/images/IMG_4556.JPG" class="img-rounded w-full" alt="">
</a>
</div>
<div class="media-body">
<h3 class="media-heading"><a href="https://github.com/paneersblog" target="_blank"><span class="text-dark">Paneer</span><small class="ml-1x">一个成熟稳重的沙雕网友</small></a></h3>
<div>搬砖永无止境</div>
</div>
</figure>
</div>
</div>
</div>
</article>
<section id="comments">
<div id="vcomments"></div>
</section>
</div>
<nav class="bar bar-footer clearfix" data-stick-bottom>
<div class="bar-inner">
<ul class="pager pull-left">
<li class="prev">
<a href="/21165.html" title="Python入门笔记(九)"><i class="icon icon-angle-left" aria-hidden="true"></i><span> 上一篇</span></a>
</li>
<li class="next">
<a href="/47766.html" title="Python入门笔记(七)"><span>下一篇 </span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
</li>
</ul>
<div class="bar-right">
<div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
</div>
</div>
</nav>
</main>
<footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
<ul class="social-links">
<li><a href="https://github.com/paneersblog" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
<li><a href="https://weibo.com/u/6203293798" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
<li><a href="https://twitter.com/Paneer_007" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
</ul>
<div class="copyright">
© 2022 Paneer & Emily
<!-- <div class="publishby">
Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
</div>-->
</div>
</footer>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/js/plugin.min.js"></script>
<script src="/js/application.js"></script>
<script>
(function (window) {
var INSIGHT_CONFIG = {
TRANSLATION: {
POSTS: '文章',
PAGES: '页面',
CATEGORIES: '分类',
TAGS: '标签',
UNTITLED: '(未命名)',
},
ROOT_URL: '/',
CONTENT_URL: '/content.json',
};
window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="//cdn.jsdelivr.net/npm/valine"></script>
<script type="text/javascript">
var GUEST = ['nick', 'mail', 'link'];
var meta = 'nick,mail,link';
meta = meta.split(',').filter(function(item) {
return GUEST.indexOf(item) > -1;
});
new Valine({
el: '#vcomments',
verify: false,
notify: false,
appId: 'IhVNOCA6KMiSCGQQEJHuNj18-gzGzoHsz',
appKey: 'M7KF1RwFGcFcF2RThCWkSCOj',
placeholder: 'Just go go',
avatar: 'mm',
meta: meta,
pageSize: '10' || 10,
visitor: false
});
</script>
<script src="//cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.js"></script>
<script>
//利用 FancyBox 实现点击图片放大
$(document).ready(function() {
$('article img').not('[hidden]').not('.panel-body img').each(function() {
var $image = $(this);
var imageCaption = $image.attr('alt');
var $imageWrapLink = $image.parent('a');
if ($imageWrapLink.length < 1) {
var src = this.getAttribute('src');
var idx = src.lastIndexOf('?');
if (idx != -1) {
src = src.substring(0, idx);
}
$imageWrapLink = $image.wrap('<a href="' + src + '"></a>').parent('a');
}
$imageWrapLink.attr('data-fancybox', 'images');
if (imageCaption) {
$imageWrapLink.attr('data-caption', imageCaption);
}
});
$().fancybox({
selector: '[data-fancybox="images"]',
hash: false,
loop: false,
});
});
</script>
</body>
</html>