"/> ">

Rumah >pembangunan bahagian belakang >Tutorial Python >if else流程判断-CLASS-11(Part 1)

if else流程判断-CLASS-11(Part 1)

PHP中文网
PHP中文网asal
2017-07-04 19:30:031271semak imbas

if判断语句

<span style="color: #008080">1</span> <span style="color: #0000ff">if</span><span style="color: #000000"> true:
</span><span style="color: #008080">2</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">true</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">3</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">4</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">false</span><span style="color: #800000">"</span>)

猜年龄大小

<span style="color: #008080">1</span> <span style="color: #008000">#</span><span style="color: #008000"> Author:dd</span>
<span style="color: #008080">2</span> age_of_boy = 56
<span style="color: #008080">3</span> age = int(input(<span style="color: #800000">"</span><span style="color: #800000">age:</span><span style="color: #800000">"</span><span style="color: #000000">))
</span><span style="color: #008080">4</span> <span style="color: #0000ff">if</span> age ==<span style="color: #000000"> age_of_boy:
</span><span style="color: #008080">5</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">you got it</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">6</span> <span style="color: #0000ff">elif</span> age ><span style="color: #000000"> age_of_boy:
</span><span style="color: #008080">7</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">you need guess smaller</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">8</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">9</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">you need guess bigger</span><span style="color: #800000">"</span>)

对输入密码进行隐藏显示

<span style="color: #008080">1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> getpass
</span><span style="color: #008080">2</span> passwd = getpass.getpass(<span style="color: #800000">"</span><span style="color: #800000">input your password:</span><span style="color: #800000">"</span>)
<span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000"> Author:dd</span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> getpass
</span><span style="color: #008080"> 3</span> 
<span style="color: #008080"> 4</span> 
<span style="color: #008080"> 5</span> _username = <span style="color: #800000">"</span><span style="color: #800000">test</span><span style="color: #800000">"</span>
<span style="color: #008080"> 6</span> _passwd = <span style="color: #800000">"</span><span style="color: #800000">111</span><span style="color: #800000">"</span>
<span style="color: #008080"> 7</span> 
<span style="color: #008080"> 8</span> username = input(<span style="color: #800000">"</span><span style="color: #800000">username:</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080"> 9</span> passwd = getpass.getpass(<span style="color: #800000">"</span><span style="color: #800000">passwd:</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">10</span> <span style="color: #0000ff">print</span><span style="color: #000000">(username,passwd)
</span><span style="color: #008080">11</span> 
<span style="color: #008080">12</span> <span style="color: #0000ff">if</span> _username == username <span style="color: #0000ff">and</span> _passwd ==<span style="color: #000000"> passwd:
</span><span style="color: #008080">13</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">user {name} login success</span><span style="color: #800000">"</span>.format(name=<span style="color: #000000">username))
</span><span style="color: #008080">14</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">15</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">login failed</span><span style="color: #800000">"</span>)
<span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000"> Author:dd</span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> getpass
</span><span style="color: #008080"> 3</span> 
<span style="color: #008080"> 4</span> 
<span style="color: #008080"> 5</span> _username = <span style="color: #800000">"</span><span style="color: #800000">test</span><span style="color: #800000">"</span>
<span style="color: #008080"> 6</span> _passwd = <span style="color: #800000">"</span><span style="color: #800000">111</span><span style="color: #800000">"</span>
<span style="color: #008080"> 7</span> 
<span style="color: #008080"> 8</span> username = input(<span style="color: #800000">"</span><span style="color: #800000">username:</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080"> 9</span> passwd = input(<span style="color: #800000">"</span><span style="color: #800000">passwd:</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">10</span> 
<span style="color: #008080">11</span> <span style="color: #0000ff">if</span> _username == username <span style="color: #0000ff">and</span> _passwd ==<span style="color: #000000"> passwd:
</span><span style="color: #008080">12</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">user {name} login success</span><span style="color: #800000">"</span>.format(name=<span style="color: #000000">username))
</span><span style="color: #008080">13</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">14</span>     <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">login failed</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">15</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">ddd</span><span style="color: #800000">"</span>)

 

Atas ialah kandungan terperinci if else流程判断-CLASS-11(Part 1). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Python中的os模块Artikel seterusnya:Python的由来与使用介绍