Learn Python the fifth day of the exercises 2019.07.18

Learn Python the fifth day of the exercises 2019.07.18

1) corresponding to the variable name is removed on both sides of the space values, and outputs the processing result

code:
name = " aleX"
if name[0]==' ':
    name=name[1:]
if name[-1]==' ':
    name=name[:-1]
print(name)
operation result:

aleX

2) determination whether the value corresponding to the variable name beginning with "al", and outputs the result

code:

name = " aleX"
if name[:2]=='al':
    print('name变量是以al开头的')
else:
    print('name变量不是以al开头的')
operation result:

variable name does not begin with the al

3) corresponding to the variable name is determined whether the value of an "X" at the end, and outputs the result

code:
name = " aleX"
if name[-1]=='X':
    print("name变量是以X为结尾的")
else:
    print("name变量不是以X为结尾的")
operation result:

X is a variable name ending with

4) the value corresponding to the variable name in the "l" is replaced with "p", and outputs the result

code:
name = " aleX"
num = len(name)
for i in range(num):
    if name[i]=='l':
        name = name.replace('l','p')
print(name)
operation result:

apeX

5) The name of the corresponding variable value in accordance with "l" division, and outputs the result

code:
name = " aleX"
new_name = name.split('l')
print(new_name)
operation result:

[' a', 'eX']

6) The name becomes a variable value corresponding to upper case, and outputs the result

code:
name = " aleX"
new_name = name.upper()
print(new_name)
operation result:

ALEX

7) The name corresponding to the variable value becomes lower case, and the output

code:
name = " aleX"
new_name = name.lower()
print(new_name)
operation result:

alex

8) Make the second character output value corresponding to the variable name

code:
name = " aleX"
print(name[1])

operation result:

a

9) Make the first three characters output value corresponding to the variable name

code:
name = " aleX"
print(name[:3])
operation result:

al

10) outputs the requested value corresponding to the variable name character 2

code:
name = " aleX"
print(name[-2:])
operation result:

eX

11) Request output value corresponding to the variable name index where "e" position

code:
name = " aleX"
num = len(name)
for i in range(num):
    if name[i] == 'e':
        print(i)
operation result:

3

12) acquisition sequences, removing the last character. Such as: oldboy then get oldbo

code:
name = " aleX"
new_name = name[:-1]
print(new_name)
operation result:

but

1. The following data is stored as a dictionary type

# # 数据:info = "name:Owen|age:18|gender:男"
# # 结果:{'name': 'Owen', 'age': 18, 'gender': '男'}
code:
info = "name:Owen|age:18|gender:男"
new_info = info.split('|')
dict_info = {}
list_info=[]
num = len(new_info)
for i in range(num):
   list_info += new_info[i].split(':')
list_num = len(list_info)
for i in range(0,list_num,2):
    dict_info.setdefault(list_info[i],list_info[i+1])
print(dict_info)
operation result:

{'name': 'Owen', 'age': '18', 'gender': '男'}

2. Complete data deduplication

# # 数据:t3 = [1, 2, 1, 2, 3, 5, 9]
# # 结果:t3 = [1, 2, 3, 5, 9]
code:
t3 = [1, 2, 1, 2, 3, 5, 9]
# 先将序列转为集合去除重复值,再将集合转回序列输出
new_t3 = list(set(t3))
print(new_t3)
operation result:

[1, 2, 3, 5, 9]

3 have the following values ​​of the set [11,22,33,44,55,66,77,88,99,90 ...]

# # 将所有大于 66 的值保存至字典的第一个key中
# # 将小于 66 的值保存至第二个key的值中
# # 即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
code:
list_str = [11,22,33,44,55,66,77,88,99,90]
k1_list,k2_list = [],[]
for i in list_str:
    if i>66:
        k1_list.append(i)
    else:
        k2_list.append(i)
dict_str = {'k1':k1_list,'k2':k2_list}
print(dict_str)
operation result:

{'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55, 66]}

4. Complete phonebook entry (OPTIONAL)

# # 需求:
# '''
# -- 从键盘中录入姓名(不区分大小写):
#   -- 姓名必须是全英文字母组成,不是则重新录入姓名,如果是q,代表退出
# -- 从键盘中再录入电话:
#   -- 电话必须为字符串中是数字(如'12312312312')且长度必须是11位
# -- 如果出现姓名相同,则保留最后一次电话号码
# -- 形成的数据是有电话分组的,如:第一次录入Owen,13355667788,则会形成
#   -- {
#       'O': {
#           'Owen': '13355667788'
#       }
#   }
# 最终数据,分组名一定大写:
# {
#     'E': {
#       'egon': '17788990000',
#       'engo': '16633445566'
#     },
#     'O': {
#       'Owen': '13355667788'
#     }
# }
# '''
code:
# 定义一个字典电话本,用来接收存储的信息
dict_nums = {}
# 定义一个信息字典,用来存储姓名和手机号码
dict_info = {}
# 定义一个用来接收新的信息的字典

# 定义个序列用来判断输入的name是否有数字
list_num = ['0','1','2','3','4','5','6','7','8','9']
# 定义一个用来判断的变量
check_name_boolen = True
# 用来判断是不是要退出整个程序
check_quit = True
check_num_boolen = True
# 定义一个判断输入姓名的函数
def check_name(name):
    global check_name_boolen
    for i in name:
        if i in list_num:
            print("你输入的name中包含了数字,请重新输入!")
            check_name_boolen = False
            break
        else:
            check_name_boolen = True
    return check_name_boolen
# 定义一个判断输入姓名的函数
def check_num(num):
    global check_num_boolen
    num_len = len(num)
    for i in num:
        if i not in list_num or num_len !=11:
            print("你输入的电话号码有误,需要重新输入!")
            check_num_boolen = False
            break
        else:
            check_num_boolen = True
    return check_num_boolen

#### 程序主题
while check_quit:
    name = input("请输入姓名(要求为大小写英文,输入数字需重新输入,按q退出):")
    # 对name值进行判断
    if name=='q':
        print("你选择了退出程序!")
        check_quit = False
        break
    check_name(name)
    while check_name_boolen != True:
        name = input("请输入姓名(要求为大小写英文,输入数字需重新输入,按q退出):")
        check_name(name)
        if name == 'q':
            print("你选择了退出程序!")
            check_quit = False
            break
    num = input("请输入手机号码(要求长度为11位且必须都为数字,输入错误需重新输入):")
    # 对输入的电话号码进行判断
    check_num(num)
    while check_num_boolen != True:
        num = input("请输入手机号码(要求长度为11位且必须都为数字,输入错误需重新输入):")
        check_num(num)

    # 到了这的时候就已经能够拿到了 有效的输入数据 name 和num.接下来要做的就是将这些数据放到字典中就行了
    new_dict = {name,num}
    old_dict = dict_nums.values()

    if dict_info == {}:
        dict_info.setdefault(name,num)
    else:
        for i in dict_info.keys():
            if i == name:
                dict_info[name] = num
        dict_info.setdefault(name, num)
    # check_quit = False
    #for i in dict_nums.keys():
    if dict_nums == {}:
        dict_nums.setdefault(name[0].upper(), dict_info)
    else:
        for i in dict_nums.keys():
             if i == name[0].upper():
                dict_nums[i] = new_dict
        dict_nums.setdefault(name[0].upper(), new_dict)
print(dict_nums)
operation result:

Please enter your name (required to write the English, enter the number to be re-entered, press q to quit): forever
Please enter your mobile number (required length of 11 and must have a digital input errors need to re-enter): 13721111111
Please enter your name ( required to write the English, enter the number to be re-entered, press q to quit): Owen
Please enter your mobile number (required length of 11 and must have a digital input errors need to re-enter): 12233445566
Please enter your name (capitalization requirements English, enter the number to be re-entered, press q to quit): q
you have chosen to exit the program!
{ 'F': { 'forever ': '13721111111', 'Owen': '12233445566'}, 'O': { 'Owen', '12233445566'}}

The last question there are problems, also need to modify

Guess you like

Origin www.cnblogs.com/foreversun92/p/11210208.html