Python--基礎学習--プロセス制御


pythonのプロセス制御について簡単に説明します
入力値の取得
linuxでパスワードを入力しても表示されないようにgetpassクラスで実現できます
もちろん隠したいだけならパスワード取得に限らない
while条件
コンテンツのループ
Trueと判断して1を実行
Falseと判断すると、2
break &continue
break:現在のループを終了します.
continue:次の内容を実行せずに、直接次のループに進みます.
def test():
    i = 0
    result = []
    while True:
        i = i + 1
        result.append(i)
        print(result)
        if i>5:
            break
        else:
            continue

test()
実行結果:
[1] [1, 2] [1, 2, 3] [1, 2, 3, 4] [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6]
if elif else条件判断
def test():
    reg = input('input your choice:1.login;2.register;3.exit:')
    ret = reg.strip()
    if ret == "1":
        print('Welcome to our family')
    elif ret == "2":
        print("You cannot register to our family,just be a visitor")
    elif ret == "3":
        exit()
    else:
        print('you must type 1 or 2 or 3')
        exit()

test()
forサイクル
パスワード入力エラーが発生したら、複数回再試行する機会を与える
username='Meta'
password='hao'
user=input('Username:')
if user == username:
    for i in range(3):
        pw=input('Password:')
        if pw == password:
            print('Welcome to our family')
            break
        else:
            print('your password is not right')
            continue
else:
    print('You r not our member,Sorry')

実行結果:
Username:Meta Password:123 your password is not right Password:123 your password is not right Password:123 your password is not right
三元演算or三目演算
条件判断の書き方
ret=a if条件判断else b
#条件がTrueと判断した場合、ret原値は変わらず、False、ret再付与値はb