if语句,while循环,for循环

[亡魂溺海] 提交于 2020-02-07 02:36:10

if语句:

分支结构,选择结构

# Author:Huan
import getpass  #引入密文密码

_username = 'Huan'
_password = 'abc'
username = input("name:")
#password = getpass.getpass("password:")
password = input("password:")

if _username == username and _password == password:
    print("Welcome user {name} login...".format(name=username))
else:
    print("Invaild username or password")

print(username,password)

 

while循环:

基于某种条件循环到结束。

# Author:Huan

age_of_huan = 26
count = 0
while count < 3:
    guess_age = int(input("guess age:"))
    if guess_age == age_of_huan:
        print("yes,yo1u got it.")
        break
    elif guess_age > age_of_huan:
        print("think smaller...")
    else:
        print("think bigger...")
    count += 1
else:
    print("you have tried too many times...")

 

for循环:

例1:

for i in range(0,10):
    if i < 5:
        print("loop",i)
    else:
        continue    #跳出本次循环,进入下次循环
    print("hello...")

例2:

# Author:Huan

age_of_huan = 26
for i in range(3):
    guess_age = int(input("guess age:"))
    if guess_age == age_of_huan:
        print("yes,you got it.")
        break
    elif guess_age > age_of_huan:
        print("think smaller...")
    else:
        print("think bigger...")
else:
    print("you have tried too many times...")

 

continue:跳过循环的这一次循环,进行循环的下一次操作

break:停止整个循环

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!