python summary and exercises (a)

python summary and exercises (a)

python summary

Related exercises

1. To achieve certification three times

Implement user to enter a user name and password when a user named seven and the password is 123, showing the successful landing, or failure of the landing!
Implement user to enter a user name and password when a user named seven and the password is 123, showing the successful landing, or landing failure, failure allowed to re-enter three times
to achieve the user to enter a user name and password when a user name and password is seven or alex 123, showing the successful landing, landed otherwise fail, allowing re-enter fails three times

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. judgment leap year

Input a year, to determine whether the year is a leap year, and outputs the result.
Note: All Year meet one of the following two conditions is a leap year. (1) it is divisible by 4 but not divisible by 100. (2) divisible by 400.

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

Note: About leap year
1 if the year divisible by four, the count in a leap year;
2 100 if the year can be divisible, is not counted as a leap year;
3 if the year divisible by 400, the count is a leap year.
100, 200, 400 is not

3. Calculate the interest rate

Assume a regular one-year interest rate of 3.25%, calculate need how many years, a million year time deposit with interest doubling?

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 output inverted triangle

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

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

5. Calculation Results

According to the sales commission to employees, to mention a stepped system, assuming a sales base salary of 3,000 yuan, 50,000 yuan less than the monthly performance, non-commissioned, 50000-100000, commission of 3%, from 100,000 to 150,000 get 5% commission 150000-250000 8%, from 250,000 to 350,000 commission of 10%, 350,000 or more, 15% commission. Users get the performance of the month from the keyboard, calculate the total amount of their salary + commission.

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)

Annotations:
Python no switch
can be achieved by a scheduling method function dictionary mapping and class
example:

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

6. Calculate the price of metro section

Beijing subway price adjusted:
6 km (inclusive) 3 million;
6-12 km (including) 4 million;
12 to 22 km (including) 5 yuan;
22 to 32 km (including) 6 million;
32 km or more portions,
each additional 1 element ride 20 km. The use of municipal transport card credit card to take rail transportation, natural multiply times the price per month per card expenses accumulated over 100 yuan after giving a 20% discount; take the time to give a 50% discount over 150 yuan later,
assuming that each month, Xiao Ming all class needs 20 days, each time to go to work back and forth once, the need to take the same route twice daily subway, programming, get away from the keyboard, to help Xiao Ming total monthly cost calculations.

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. Calculate cell rebound

Ball free fall from a height of 100 meters, the anti-jump back after each landing half its original height; down again, find it at the 10th floor, after a total of how many meters? 10th rebound tall?

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. Log in to write interfaces

Allowing users to enter a user name and password
authentication is successful after the welcome message
to exit the program after unsuccessful attempts to
support multiple users login (prompted by a list of store multiple account information)
after the user authentication fails three times, quit the program, start the program attempts to log on again when, or locked state (Hint: the need to lock the user's state saved in a file)

#使用一个计数器,专门记录同一个用户登录的次数,为三时,用户信息将状态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)

Guess you like

Origin blog.csdn.net/xgy123xx/article/details/92605501