python练习3

来源:互联网 发布:fx2da模块数据 编辑:程序博客网 时间:2024/06/10 16:55
1.import time
1). Creat :
2). Login :
3). Delete :
4). View :
登陆时判断本次登陆与上次登陆时间的差,如果time<4h,显示,你已经登陆在4小时前。
import time
hour = 4 * 60 * 60
USER = {}
def creat():
    name1 = raw_input('new username:')
    if name1 not in USER.keys():
        pass1 = raw_input('new passwprd:')
        USER[name1] = [pass1,0]
    else:
        print 'the username is existed,please give me a new name!'
        creat()
def login():
    if USER.keys() == 0:
        print 'no users exist,please create user'
    else:
        user_in = raw_input('username:')
        pass_in = raw_input('password:')
        d1 = time.time()
        if USER[user_in][1] == 0:
            if user_in in USER.keys() and pass_in == USER[user_in][0]:
                USER[user_in][1] = d1
                print 'welcome to user system!'
            else:
                print 'error username or password!'
        else:
            if user_in in USER.keys() and pass_in == USER[user_in][0]:
                if d1 - USER[user_in][1] >= hour:
                    print 'wlecome to user system!'
                else:
                    d2 = (hour - (d1 - USER[user_in][1])) / 60
                    print 'you have been login within 4h ,please login after %d minutes' % d2
            else:
                print 'error username or password!'
def delete():
    user1 = raw_input('username:')
    USER.pop(user1)
    print 'delete user successful'
def view():
    print USER.keys()
def main():
    cho = 'Enter your choice:'
    print '''
    you can input:
    creare(c) login(l)  delete(d) view(v)
    '''
    while True:
        pro = raw_input(cho).lower()
        if pro == 'c':
            creat()
        elif pro == 'l':
            login()
        elif pro == 'd':
            delete()
        elif pro == 'v':
            view()
        else:
            print 'error:you can input c l d v'
main()



2.购物车程序需求:

    1)启动程序后,让用户输入账户金额,然后打印商品列表;
          2)允许用户根据商品编号购买商品;
        3)用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒;
          4)可随时退出,退出时打印已购买的商品和余额;
shopinfo = [['iphone', 5800],['book', 100],['bike', 200]]
BUY =[]
def buy():
    a = input('please input your money:')
    print 'shopping list:'
    for num, goods in enumerate(shopinfo):
        print  num, goods[0],goods[1]
    num1 = input('please input goods number:')
    if a < shopinfo[num1][1]:
        print 'your money is not enough!!'
    else:
        a1 = a - shopinfo[num1][1]
        BUY.append(shopinfo[num1][0])
        print 'your balance:%d\nyour goods:%s ' % (a1,shopinfo[num1][1])
def main():
    while True:
        d = '''
        *************** welcome to the shopping cart system ****************
        please choice:
            (B)uy
            (Q)uit
        '''
        print d
        pro = raw_input('please input your choice:').lower()
        if pro == 'q':
            print 'Quit sccessful'
            break
        elif pro == 'b':
            buy()
        else:
            print 'please input B or Q'
main()



3.定义一个函数func(*args),该函数效果如下:

func(222,111,’xian’,’hahaah’) –> ‘xian’
func(7,’name’,’fensi’) —>’fensi’
func(1,2,3,4) —> None

def func(*args):
    if isinstance(args[2], str):
        return args[2]
    return None
print func(222, 111, 'xian', 'hahaah')
print func(7, 'name', 'fensi')
print func(1, 2, 3, 4)


4.定义一个func(name=None,**kwargs),该函数的效果为:

func('Liyulong') –> 'name:Liyulong'
func('Liyulong',age=22) –> "name:Liyulong, age:22"
func('Liyulong',age=22,weight=50) –> "name:Liyulong,age:22,weight:50"

def func(name=None, **kwargs):
    print '\nname:', name,
    for k, v in kwargs.items():
        print ',', '%s:%s' % (k, v),

func('Liluylong')
func('Liluylong',age=22)

func('Liluylong',age=22,weight=50)