Pythonの概要と演習(A)

Pythonの概要と演習(A)

Pythonの概要

関連演習

1.認証を3回達成するために

7という名前のユーザーとパスワードが成功着陸、または着陸の失敗を示し、123であるときに、ユーザー名とパスワードを入力するようにユーザを実装!
7という名前のユーザーとパスワードが成功着陸、または着陸を示し、123であるときに、ユーザー名とパスワードを入力するようにユーザを実装障害が、失敗が3回の再入力を許可され
たユーザ名とパスワードは、アレックス123 7であるとき、またはユーザー名とパスワードを入力するようにユーザを達成するために、成功した着陸を示し、そうでない場合は再入力することができ、失敗上陸3回失敗

username = "seven"
password = "123"
count = 1
while  count <= 3:
    name = input("name>>:").strip()
    psword = input("password>>:").strip()
    if username == name and psword == password:
        print("登录成功")
        break
    else:
        print("登录失败")
        count += 1

2.判定うるう年

入力年は、年がうるう年で、その結果を出力するかどうかを判断します。
注:すべての年は、次の2つの条件のいずれかを満たすことはうるう年です。(1)それが4で割り切れるが、100で割り切れません。(2)400で割り切れます。

years = int(input("year:"))
if years % 4 == 0 and years % 100 != 0 or years % 400 == 0:
    print(years,"是闰年")
else:
    print(years,"不是闰年")

注:閏年について
1 4で割り切れる年は、うるう年のカウント場合は、
年が、うるう年として割り切れカウントされないことができる場合は2 100;
3年割り切れる場合は400で、カウントはうるう年です。
100、200、400ではありません

3.金利を計算します

3.25パーセントの定期的な1年間の金利を想定し、計算は、何年金利倍増と百万年の定期預金が必要ですか?

interest_rate = 0.0325
capital = 10000
total_money = capital*(1+interest_rate)
total_years = 1
while total_money <= 20000:
    print("years:%d money:%d"%(total_years,total_money))
    total_money *= 1+interest_rate
    total_years += 1

4.while出力逆三角形

使用while,完成以下图形的输出

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
count = 0
while count < 9:
    if count < 5:
        count += 1
        print("*"*count)
    else:
        count += 1
        print("*"*(9-count))

5.計算結果

販売手数料の従業員によると、100,000〜150,000の3000元の販売基本給、毎月のパフォーマンス、非委託、50000から100000未満5万元、3%の手数料を、と仮定すると、ステップのシステムを言及します10%250,000 350,000手数料35万以上、15%のコミッションから5%の手数料150000から250000 8%を取得。ユーザーは、自分の給料+歩合の合計金額を計算し、キーボードから月のパフォーマンスを得ます。

fundamental_wages = 3000
achievement = int(input("outstanding:").strip())  #业绩
salary = 0
if achievement < 50000:
    salary = fundamental_wages
elif achievement <= 100000:
    salary = (1+0.03)*fundamental_wages
elif achievement <= 150000:
    salary = (1+0.05)*fundamental_wages
elif achievement <= 250000:
    salary = (1+0.08)*fundamental_wages
elif achievement <= 350000:
    salary = (1+0.1)*fundamental_wages
else:
    salary = (1+0.15)*fundamental_wages
print("salary:%d"%salary)

注釈:
Pythonのないスイッチは、
スケジューリング方法の機能辞書マッピングとクラスによって達成することができない
例:

switch = {
    "a":lambda x: x*3,
    "b":lambda x: x*2,
    "c":lambda x: x**2,
}

6.メトロセクションの価格を計算

北京の地下鉄の価格調整:
6キロ(含む)300万;
400万(含む)6〜12キロ、
5元(含む)12〜22キロ;
600万(含む)22〜32キロメートル、
32 km以上の部分は、
各追加の1つの要素20キロに乗ります。鉄道輸送、20%の割引を与えた後、100元の上に蓄積されたカードの費用あたり月額自然乗算倍の価格を取るために市営交通カードのクレジットカードの使用;、後に150元を超える50%の割引を与えるために時間がかかり
、各月と仮定し、暁明すべてクラスは、暁明毎月の総コスト計算を支援するために、、、20日に1回、前後に仕事に行くために、各時間を必要と同じルート一日二回、地下鉄を取る必要があり、プログラミング、離れて、キーボードから取得します。

total_expense = 0
times = 1
while times <= 20:
    distance = int(input("distance>>:").strip())
    if distance <= 6:
        per_times_expense = 3
    elif distance <= 12:
        per_times_expense = 4
    elif distance <= 22:
        per_times_expense = 5
    elif distance <= 32:
        per_times_expense = 6
    else:
        #向上取余  多出来的部分,如果小于20km则算1元,
        surplus = distance-32
        if surplus%20 == 0:
            per_times_expense = 6 + surplus//20
        else:
            per_times_expense = 6 + surplus//20 + 1
    #判断是否可以使用交通卡优惠
    if total_expense < 100:
        total_expense += per_times_expense
        print("total:%d   per expense:%d"%(total_expense,per_times_expense))
    elif total_expense < 150:
        total_expense += per_times_expense*0.8
        print("total:%d   per expense:%d"%(total_expense,per_times_expense*0.8))
    else:
        total_expense += per_times_expense*0.5
        print("total:%d   per expense:%d"%(total_expense,per_times_expense*0.5))

7.細胞リバウンドを計算

バックそれぞれが半分の元の高さを着陸後、ボール無料100メートルの高さから落下、抗ジャンプ;ダウン再び、何メートルの合計後、10階でそれを見つけますか?10リバウンド背の高いですか?

times = 1
hight = 100.0
distance = 0.0
while times <= 10:
    distance += hight
    hight /= 2
    print("times:%d distance:%.2f hight:%.2f"%(times,distance,hight))
    times += 1

8.インターフェースを記述するためにログイン

ユーザーは、ユーザー名とパスワードの入力を可能に
ウェルカムメッセージの後に認証が成功する
ために失敗した試行の後、プログラムを終了するには、
複数のユーザーをサポートするには(店舗、複数のアカウント情報のリストによって促さ)ログイン
ユーザ認証に3回失敗した後、プログラムを起動し、プログラムを終了して、再度ログオンしようとしますとき、またはロック状態(ヒント:ファイルに保存され、ユーザーの状態をロックする必要があります)

#使用一个计数器,专门记录同一个用户登录的次数,为三时,用户信息将状态isLock设置为1
#因为这个程序,循环最多执行三次,如果三次全部是同一个用户验证密码,则锁定这个用户
#存储用pickle
import pickle
count = 0
# userinfo_dic = {
#     "xiaoming":{"password":"123","isLock":0},
#     "xiaohua":{"password":"321","isLock":0}
# }
userinfo_dic = {}
login_times = 0
old_name = ""
with open("./userinfo.txt","rb") as  f:
    userinfo_dic = pickle.load(f)
print(userinfo_dic)
while count < 3:
    #用户输入用户名,密码
    username = input("name:").strip()
    psword = input("password:").strip()

    #获取用户信息
    userinfo = userinfo_dic.get(username, False)
    if userinfo:  #用户是否存在
        if not userinfo.get("isLock"):  #状态不为锁定状态
            password = userinfo.get("password")
            if psword == password:
                print("登录成功,欢迎%s"%username)
                break
            else:
                if old_name == username: #判断是否和上次为同一个用户名,是则记录登录次数,否则
                    login_times += 1
                else:
                    old_name = username
                    login_times = 1
                print("密码错误")
        else:
            print("用户已被锁定")
    else:
        print("用户名不存在")
    count += 1
else:
    print("错误次数过多!")
    print(login_times)
    if login_times >= 3:
        userinfo_dic[username]["isLock"] = 1
    with open("./userinfo.txt","wb") as  f:
        pickle.dump(userinfo_dic,f)

おすすめ

転載: blog.csdn.net/xgy123xx/article/details/92605501