码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习之路(十):实例整理

时间:2020-02-24 14:40:37      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:鼠标   登陆验证   用户密码   username   比较   result   失败   input   一个   

1.while

 1 count = 1
 2 while count <= 3:
 3     print(count)
 4     user = input(请输入您的姓名)
 5     psw = input(请输入您的密码)
 6     if user == newboy and psw == pushu666:
 7          print(登陆成功)
 8          break
 9     else:
10         print(登陆失败)
11     if count == 3:
12         choice = input(请输入是否需要继续(Y/N):)
13         if choice == Y:
14             count = 1
15             continue
16         elif choice == N:
17              break
18         else:
19              print(输入错误)
20         break
21     count = count + 1
 1 count = 1
 2 while count <= 3:
 3     print(count)
 4     user = input(请输入您的用户名)
 5     psw = input(请输入您的密码)
 6     if user == newboy and psw == pushu666:
 7         print(登陆成功)
 8         break
 9     else:
10         time = 3 - count
11         template = 密码或用户名错误,您还剩余%s次机会。 %(time,)
12         print(template)
13     count = count + 1
 1 count = 2
 2 while count >= 0:
 3     user = input(请输入您的用户名)
 4     psw = input(请输入您的密码)
 5     if user == newboy and psw == pushu666:
 6         print(欢迎登陆)
 7         break
 8     template = 密码或用户名错误,您还剩余%s次机会。 % (count,)
 9     print(template)
10     count -= 1
11 else:
1 ount = 1
2 while count <= 10:
3     fen = int(input(请第%s号评委打分: % (count,)))
4     if fen > 10 or fen <0:
5         print(请重新输入!)
6         continue
7     else:
8         print(第%s为评委打分为:%s% (count, fen,))
9     count += 1

2.数据类型相关

1 # 输出所有键值,并且根据输入的键输出相应的值:
2 info = {name: 卡戴珊, age: 18, gender: , hobby: 同志}
3 for k, v in info.items():
4     print(k, v)
5     key = input(请输入属性:)
6 print(info[key])
1 # 给出一个空字典,在字典中加入相应的键值k1=1,k2=2,k3=3.
2 info = {}
3 info[k1] = 1  # 此时加入的k1的值为字符串,而k2,k3为数字
4 info[k2] = 2
5 info[k3] = 3
6 print(info)
1 # 给出一个空字典,在字典中加入用户输入的key和value:
2 info = {}
3 k = 0 
4 while k <= 3:
5     key = input(请输入key:)
6     value = input(请输入value:)
7     info[key] = value
8     k = k + 1
9 print(info)
1 # 请用代码实现 ‘k1|v1, k2|v2, k3|v3, k4|v4‘ ---> {‘k1‘:‘v1‘, ‘k2‘:‘v2‘, ‘k3‘:‘v3‘, ‘k4‘:‘v4‘}
2 
3 info = {}
4 message = k1|v1,k2|v2,k3|v3,k4|v4
5 for i in message.split(,):
6     v1, v2 = i.split(|)
7     info[v1] = v2
8     print(info)
 1 # 构建用户列表,然后让用户进行登陆验证
 2 """
 3 user_list = []
 4 while True:
 5     k = input(‘请输入用户名:‘)
 6     if k == ‘N‘:
 7         break
 8     v = input(‘请输入密码:‘)
 9     info = {}
10     info[‘user‘] = k
11     info[‘psw‘] = v
12     user_list.append(info)
13 print(user_list)
14 username = input(‘请输入登陆名:‘)
15 password = input(‘请输入用户密码:‘)
16 message = ‘登陆失败‘
17 for items in user_list:
18     if items[‘user‘] == username and items[‘psw‘] == password:
19         message = ‘登陆成功‘
20         break
21 print(message)
 1 #  将11,22,33,44,55,66,77,88,99,111中大于66的数字存放在字典键k1下,其余存在k2下
 2 """
 3 li = [11, 22, 33, 44, 5, 66, 77, 88, 99, 111]
 4 dic = {‘k1‘:[], ‘k2‘:[]}
 5 for i in li:
 6     if i == 66:
 7         continue
 8     elif i > 60:
 9         dic.setdefault(‘k1‘).append(i) #如果default中没有value就只进行查询操作
10     else:
11         dic.setdefault(‘k2‘).append(i)
12 print(dic)
 1 # 购物车
 2 goods = [{name: 电脑, price:1999},
 3          {name: 鼠标, price:10},
 4          {name: 游艇, price:20},
 5          {name: 美女, price:998}, ]
 6 while 1:
 7     for i in goods:
 8         print(goods.index(i) + 1, i[name], i[price])
 9     str_input = input(请输入您要选择的商品序号,输入Q/q退出:)
10     if str_input.isdigit() and 0 < int(str_input) < len(goods):
11         i_index = int(str_input) - 1
12         print(goods[i_index][name], goods[i_index][price])
13     elif str_input.upper() == Q:
14         break
15     else:
16         print(输入错误,请重新输入!)
1 # 水仙花数
2 num = input(请输入一个数:)
3 s = 0
4 for c in num:
5     s = int(c) ** 3 + s
6 if int(num) == s:
7     print(水仙花数)
8 else:
9     print(不是,请重新输入)
 1 # 排序(不用sort):冒泡排序
 2 lst = [1, 3, 6, 8, 5, 11]
 3 count = 0
 4 while count < len(lst):
 5     i = 0
 6     while i < len(lst) - 1:
 7         if lst[i] > lst[i+1]:
 8             lst[i], lst[i+1] = lst[i+1], lst[i]
 9         i = i+1
10     count += 1
11 print(lst)

3.函数

 1 # 打印索引为基数的元素
 2 def func(lst):
 3     result = []
 4     for i in range(len(lst)):
 5         if i % 2 == 1:
 6             result.append(lst[i])
 7     return result
 8 
 9 
10 ret = func(["皇阿玛", "纪晓岚", "河身", "小六子"])
11 print(ret)

1 lst = ["皇阿玛", "纪晓岚", "河身", "小六子"]
2 def func(lst):
3     return lst[1::2]
4 print(func(lst))

 

 1 # 计算字符串中数字、字母以及空格数量
 2 def func(s):
 3     shuzi = 0
 4     zimu = 0
 5     kongge = 0
 6     qita = 0
 7     for i in s:
 8         if i.isdigit():
 9             shuzi += 1
10         elif i.isalpha():
11             zimu += 1
12         elif i ==  :
13             kongge += 1
14         else:
15             qita += 1
16     return shuzi, zimu, kongge, qita
17 print(func(s = dsafwef  wef907987fa))
1 # 比较大小
2 def func(a, b):
3     return a if a > b else b
4 print(func(10, 90))
 1 # 将字典中值的长度大于2的键值对装入新字典
 2 def func(dic):
 3     new_dic = {}
 4     for k, v in dic.items():
 5         if len(v) > 2:
 6             s = v[0:2]
 7             new_dic[k] = s
 8         else:
 9             new_dic[k] = v
10     return new_dic
11 print(func({jay:周杰伦, 潘婷:洗发水, 赌神: 周润发}))

 

4.文件

# 在指定文件中写入信息
def func(name, gender, age, edu):
    with open("student_msg", mode="a", encoding=utf-8) as f:
        f.write(name+"_"+gender+"_"+age+"_"+edu+"\n")
        return name, gender, age, edu
name = input("请输入姓名:")
gender = input("请输入性别:")
age = input("请输入年龄:")
edu = input("请输入学历:")
print(func(name, gender, age, edu))
 1 def func(name, age, edu, gender=""):
 2     with open("student_msg", mode="a", encoding=utf-8) as f:
 3         f.write(name+"_"+gender+"_"+age+"_"+edu+"\n")
 4         return name, gender, age, edu
 5 while 1:
 6     name = input("请输入姓名:")
 7     gender = input("请输入性别:")
 8     age = input("请输入年龄:")
 9     edu = input("请输入学历:")
10     if gender == "":
11         print(func(name, age, edu))
12     else:
13         print(func(name, age, edu, gender))

 

Python学习之路(十):实例整理

标签:鼠标   登陆验证   用户密码   username   比较   result   失败   input   一个   

原文地址:https://www.cnblogs.com/Studying-Du/p/12356841.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!