2019.07.19 (day06) learn the job (programming)

day06 HomeWork

Topic one:

1. Define a function that can be implemented within an information input, how the information can not be converted to a positive integer, then re-enter until the energy is converted to a positive integer, the conversion of foreign returns a positive integer

def get_int():
    while True:
        data =input('请输入一个数:')
        if data.isdigit():# 判断输入的数是不是数字
            return int(data)
res = get_int()
print(res)

2. Define a function that can be implemented enter a message inside, how this information can not be converted to a negative integer, then re-enter until the energy is converted to a negative integer, then return to the external negative integer conversion

def get_int():
    while True:
        data = input('data: ')
        if data.isdigit():
            return int(-1*data)
        elif data.startswith('-') and data[1:].isdigit() and data.count('-') == 1:
            num = data.split('-')[1]
            if num.isdigit():
                return int(data)
            else:
                num = data.split('.')
                num = str(num[0])
                return num
        else:
            num = data.split('.')
            num=str(num[0])
            return int(num)*-1
res = get_int()
print(res)

3. Define a function to achieve the outside world to pass a message, whether internal judge can be converted to a positive integer, returns True | Flase information

def get_int():
    data=input('data:')
    if data.startswith('-') and data[1:].isdigit() and data.count('-') == 1:
        return True
    if data.isdigit():
        return True
    else:
        return False
res = get_int()
print(res)

4. Define a function to achieve the outside world to pass a message, whether internal judge can be converted to a negative integer, returns True | Flase information

def get_int():
    data = input('data:')
    if data.startswith('-') and data[1:].isdigit() and data.count('-') == 1:
        return True
    if data.isdigit():
        return True
    if '.'in data:
        return True
    else:
        return False
res = get_int()
print(res)

The definition of a function realized passing an integer number, and determines the direct printing is odd or even number if

def get_int(data):
    if data%2 ==0:
        print('data是个','偶数')
    else:
        print('data是个', '奇数')
get_int(101)

6. Write a function, check the length of the list passed, if more than 2, then only preserve the contents of the first two lengths, and returns the new content to the caller.

def get_list(data):
    res=[]
    ls=['xichen','beautiful','xuchen','height','weight']
    if data > 2:
        print(ls[:2])
        return ls[2:]
    else:
        print(ls)
res=get_list(3)
res=get_list(2)

7. Write a function to check all get the odd bit index corresponding elements of the incoming list or tuple object, and returns it to the caller as a new list.

def get_list(data):
    ls = [ ]
    for i in range(len(data)):
        if i % 2 == 1:
            ls.append(data[i])
    print(ls)
data = ['xu','chen','cheng','yu','cheng','xing']
res = get_list(data)

8. Define a function, simply pass "k1: v1, ..., kn: vn" fixed format such string can be converted to { 'k1': 'v1', ..., 'kn ':' vn '} dictionary and returns this

def get_dict(data):
    dic1 = dict()
    ls = t.split(',')
    for i in ls:
        ls2=i.split(':')
        dic1[ls2[0]]=ls2[1]
    print(dic1)
t="k1:v1,k2:v2,k3:v3,k4:v4,kn:vn"
get_dict(t)

9. simple shopping cart, requirements are as follows (you can also use the function without): # achieve Print product details, user name and enter the product number to purchase, then trade name, price, number of buy add to the list, if the input is empty or other illegal input requires the user to re-enter, print information added to the shopping cart after the purchase is successful.

def get_yes():
    while True:
        ls=[]
        msg_dic={'apple':10,'tesla':100000,'mac':3000,'lenovo':30000,'chicken':10,}
        for i in msg_dic.items():
            tu=i
            print('name:',tu[0],'price:',tu[1])
        for i in msg_dic.items():
            name = input('商品>>:')
            num = input('购买个数>>:')
            if name in msg_dic and num.isdigit():
                ls.append((name, msg_dic[name], num))
                print(ls)
            else:
                continue
res=get_yes()
print(res)

Guess you like

Origin www.cnblogs.com/xichenHome/p/11221768.html