zoukankan      html  css  js  c++  java
  • python之路----1

    今天刚注册了一个账号,先试试,放一两个以前写的小程序:

    1.输出名片

    name = raw_input('Please input your name: ')
    age =int(raw_input('Please input your age: '))
    job = raw_input('Please input your job: ')
    msg='''
    Infomation of user %s:
    —————Start————
    Name : %s
    Age  : %d
    Job  : %s
    —————End—————'''% (name,name,age,job)
    print (msg)

    2.密码设置隐藏

    import getpass #保护密码,只要不打印就显示不出来密码
    username = raw_input('username:')
    password = getpass.getpass('password:')
    print(username,password)

    3.密码设置隐藏(升级优化)

    import getpass
    name = raw_input('请输入用户名:')
    pwd = getpass.getpass('请输入密码:')
    if name == 'lucia' and pwd =='cmd':
        print('hello!')
    else:
        print('出错啦!')

    4.一个小登陆程序

    user = 'lucia'
    passwd = 'gohome'
    username = raw_input('username:')
    password = raw_input('password:')
    if user == username:
        print 'username is correct...'
        if password == passwd:
            print 'welcome login...'
        else:
            print'password is invalid...'
    else:
        print'连用户名都没对,一边呆着去。。。'

    5.猜年龄游戏
    age = 22
    guess_num = int(raw_input('input your guess num:'))
    if guess_num == age:
        print'congratulations! you got it!'
    elif guess_num > age:
        print'think smaller!'
    else:
        print'think larger'

    6.猜年龄游戏_优化1

    age = 22
    for i in range(10):
        guess_num = int(raw_input('input your guess num:'))
        if guess_num == age:
            print'congratulations! you got it!'
            break
        elif guess_num > age:
            print'think smaller!'
        else:
            print'think larger'

    7.猜年龄游戏_优化2
    age = 22
    for i in range(10):
        if i < 3:
            guess_num = int(raw_input('input your guess num:'))
            if guess_num == age:
                print'congratulations! you got it!'
                break
            elif guess_num > age:
                print'think smaller!'
            else:
                print'think larger'
        else:
            print'too many attemps...bye!'
            break

    8.猜年龄游戏_优化3

    age = 22
    counter = 0
    for i in range(10):
        if counter < 3:
            guess_num = int(raw_input('input your guess num:'))
            if guess_num == age:
                print'congratulations! you got it!'
                break
            elif guess_num > age:
                print'think smaller!'
            else:
                print'think larger'
        else:
            continue_confirm = input('do you want to continue:')
            if continue_confirm == 'y':
                counter = 0
                continue
            else:
                print('bye')
                break

    9.登陆三次小程序

    import getpass
    user = 'lucia'
    pwd = 123
    username = raw_input('Please input your name:')
    password = getpass.getpass('Please input your password:')
    i = 0
    if i < 3:
        i += 1
        if username == user and password == pwd:
            print 'Welcome login....'
        else:
            print'your name or password invalid...'
            continue

    10.看某数是否在该列中,找到后进行替换,有多次出现的话用for循环进行多次替换

    name = [1,2,3,4,5,6,7,8,9,1,2,3,5,6,8,1,2,3,6,1,4]
    if 4 in name:
        ele_num = name.count(4)
        ele_position = name.index(4)
        print 'num is %d,position is %s' %(ele_num,ele_position)
        print name
    for i in range(ele_num):
        ele_position = name.index(4)
        name[ele_position] = 40
        print name

    11.字符串格式化

    msg = 'hahah{0},dddd{1}'
    print msg.format('_lucia','_home')#格式化
    print (msg[1:2])#切片
    print(name.center(40,'-'))#将字符串居中,两边用横杠填充
    print(name.find('c'))#找相关字符,若有是index,若没有就是-1

    12.判断输入是否为数字,若为字母就出错。
    age = raw_input('your age:')
    if age.isdigit():
        age = int(age)
    else:
        print 'invalid data type!'

    13.字符串部分函数

    name = 'alexsdf' 
    print(name.isalnum())  #判断是否有特殊字符
    print(name.endswith('sss'))  #判断是否以某个字符串结束
    print(name.startswith('alex')) #判断是否以某个字符串开始
    print(name.upper())  #某个字符串转换成大写
    print(name.lower())  #某个字符串转换成小写
    name1 = ' lucia  '
    print name1.strip() #去掉字符串两边的空格

    14.字典函数实例

    id_db = {
        612123198110266124:{
            'name':'lucia',
            'age' :32,
            'addr':'shaanxi'
        },
        612123198110266324:{
            'name':'luca',
            'age' :31,
            'addr':'shanxi'
        },
        602123198310266125:{
            'name':'lua',
            'age' :32,
            'addr':'shandong'
        },
    }
    print id_db
    id_db[602123198310266125]['name'] = 'luciaaa'#修改字典里面的名字
    del id_db[602123198310266125]['age']#用del删掉字典里面的年龄
    id_db[612123198110266324].pop('name')#用pop删掉字典里面的姓名
    print (id_db[602123198310266125])
    print (id_db[612123198110266324])
    print(id_db.setdefault(612123198110266124))#取一个key,如果存在就取出value
    print(id_db.setdefault(61212319811026612455))#取一个key,如果不存在就创建一个key,value为none
    print(id_db.setdefault(37147119930614363223434,"hahha"))#给创建的key赋值
    print(id_db.fromkeys([1,2,3,8,9,5,6,7],'lucia'))#将列表里面的每个数排序后都赋值为lucia

    15.for 显示所有的key和value
    for key in id_db:
        print key,id_db[key]

    16.购物小程序

    salary = raw_input('Input your salary:')
    if salary.isdigit():
        salary = int(salary)
    else:
        exit('Invilid data type...')

    welcome_msg = 'Welcome to Lucia shopping mall'.center(50,'-')
    print(welcome_msg)

    exit_flag = False
    product_list=[
        ('iphone',5888),
        ('mac air',8000),
        ('mac pro',9000),
        ('xiaomi',19.9),
        ('coffee',30),
        ('tesla',820000),
        ('bick',700),
        ('cloth',200)
    ]

    shop_car = []
    while exit_flag is not True:
        print ('product list'.center(50,'*'))
        for item in enumerate(product_list):
            index = item[0]
            p_name = item[1][0]
            p_price = item[1][1]
            print(index,'.',p_name,p_price)
       
        user_choice = input('[q=qiut,c=check]What do you want to buy?')
        if user_choice.isdigit():#首先是选择商品
            user_choice = int(user_choice)
            if user_choice < len(product_list):#判断商品编号是否小于列表长度
                p_item = product_list[user_choice]
                if p_item[1] <= salary: #判断是否买得起
                    shop_car.append(p_item)#放入购物车
                    salary -=  p_item[1]  #扣钱
                    print('Added [%s] into shop_car,your balance is [%s]'%
                         (p_item,salary))
                else:
                    print('Your balance is [%s],cannot afford this...'% salary)
        else:
            if user_choice == 'q' or user_choice =='quit':
                print('Purchased product as below.center(40,'*')')
                for item in shop_car:
                    print item
                print('END'.center(40,'*'))
                print('Your balance is [%s]'% salary)
                print('Bye')
                exit_flag = True
            elif user_choice == 'c' or user_choice =='check':
                print('Purchased product as below.center(40,'*')')
                for item in shop_car:
                    print item
                print('END'.center(40,'*'))
                print('Your balance is [%s]'% salary)

  • 相关阅读:
    Modal的跳转方法为什么会显得那么奇怪
    新博客介绍
    Swift弹窗
    Java 定时任务之Quartz
    40个Java集合面试问题和答案
    elasticsearch 学习笔记
    Mac使用指南
    平时学习遇到问题及解决方法
    session和request的区别
    框架中web.xml中配置文件解析
  • 原文地址:https://www.cnblogs.com/lucia8522/p/6678907.html
Copyright © 2011-2022 走看看