zoukankan      html  css  js  c++  java
  • 循环、格式化输出、数据统计

    主要内容:
      for循环

      while循环

      格式化输出(2)

      数据统计及记录

    #############################################################

    1、for循环

    (A):

    # #one_guess_age
    # num_age = 40
    # for i in range(3):
    # age_input = int(input("input age:"))
    # if num_age == age_input:
    # print ("OK")
    # break
    # elif age_input < num_age:
    # print("smaller")
    # else:
    # print("bigger")
    # else:
        #for循环正常执行则执行else中的内容

    # exit("too many attempt")
    # print("welcome two")

    验证:
    F:PythonPython3python.exe D:/python培训/our_python/day2/test.py
    input age:0
    smaller
    input age:50
    bigger
    input age:40
    OK
    welcome two

    (B)

    # for i in range(3):
    # if i < 2:
    # continue
    # # continue退出本次循环,继续下一次循环
    # else:
    # for j in range(10):
    # if j<8:
    # continue
    # # continue退出本次循环,继续下一次循环
    # else:
    # print(i,j)

    验证:
    F:PythonPython3python.exe D:/python培训/our_python/day2/test.py
    2 8
    2 9

    2、while循环

    #while循环
    count = 0
    oldboy_age=56

    while count < 3:
    age_input = input("age>>:")
    if age_input.isdigit():
    age_input = int(age_input)
    else:
    count += 1
    continue
    if age_input == oldboy_age:
    print("OK")
    break
    elif age_input > oldboy_age:
    print("try small")
    else:
    print("try big")
    count +=1
    name = ["张三","李四","王五","赵六"]
    name1 = name.copy()
    name2 = name
    print(name,id(name),"原值"," ",name1,id(name1),"copy值"," ",name2,id(name2),"赋值")

    验证:

    F:PythonPython3python.exe D:/python培训/our_python/day2/test.py
    age>>:56
    OK
    ['张三', '李四', '王五', '赵六'] 2561688 原值
    ['张三', '李四', '王五', '赵六'] 2525864 copy值
    ['张三', '李四', '王五', '赵六'] 2561688 赋值

    3、格式化输出(2)

    msg='hello word'
    print(msg[4])
    #首字母大写
    print(msg.capitalize())
    #输出占用20个位置,居中显示,*号占位,默认空格占位
    print(msg.center(20,'*'))
    #统计第4位到第10位l的个数
    print(msg.count('l',4,10))
    #检测字符串是否以某个字符结尾
    print(msg.endswith('d'))

    msg1='a b'
    #指定tab键占用的空格数
    print(msg1.expandtabs('10'))
    #检测字符串中某个字符的位置
    print(msg1.find('d',1,10))

    # format()
    print('{} {}'.format('name','age','sd'))
    print('{name}'.format(name='zs'))
    print('{0} {1} {0}'.format('name','age'))
    4、数据统计及记录
    #统计列表
    a = [
    ['Ipone', 5800],
    ['bike', 2000],
    ['Coffee', 30],
    ['Coffee', 30],
    ['Coffee', 30],
    ['Coffee', 30]
    ]
    b = {}
    for i in range(len(a)):
    b[a[i][0]]=a.count(a[i])
    print(b)

    #统计字典
    Dict = {'Ipone': 1, 'bike': 1, 'Coffee': 4}
    for k,v in Dict.items():
    print(k,v)

    #输出系统当前时间
    import time
    print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    #将字典中的数据写入文件,更新时将上次数据直接覆盖
    Dict = {'Coffee': 5, 'Ipone': 1, 'bike': 1}
    with open('C:\Users\admin\Desktop\shopping_Dict.txt', 'w') as D:
    D.writelines([
    "%s:%d;" % item
    for item in Dict.items()
    ])
    print (Dict)

    #将文件中的数据读入字典
    f = open('C:\Users\admin\Desktop\shopping_Dict.txt', 'r')
    f.seek(0,0)
    for line in f.readlines():
    print("上次购买商品为[商品名:数量]:")
    L=list(map(str, line.split(';')))
    # print(L)
    for i in range(len(L)):
    print (L[i])
    f.close()

    #不同人的结果输入到不同的文件
    import time
    name_input = input("please input Username:")
    salary = input("money>>:")
    date = time.strftime("%Y%m%d%H%M%S", time.localtime())
    shopping_Dict = {'Coffee': 5, 'Ipone': 1, 'bike': 1}

    f1 = "C:\\Users\\admin\\Desktop\\shopping_Dict_%s.txt" % name_input
    SD = open(f1,'w')
    SD.writelines(["%s:%d;" % item for item in shopping_Dict.items()])
    SD.close()

    f2 = "C:\\Users\\admin\\Desktop\\shopping_Dict_DATE_%s.txt" % name_input
    DD = open(f2,'w')
    DD.writelines("购买时间为:%s;" % date)
    DD.close()

    #balance
    f3 = "C:\\Users\\admin\\Desktop\\shopping_Dict_balance_%s.txt" % name_input
    BD = open(f3,'w')
    BD.writelines("卡上余额为:%s;" % salary)
    BD.close()
    ###############################################################################################
    #不同人登录、输出各自的购买信息;首次登录或者未购买物品则输出为“目前还没有购买过任何物品”
    import os
    # name_input = input("please input Username:")
    f1 = "C:\\Users\\admin\\Desktop\\shopping_Dict_%s.txt" % name_input
    if os.path.exists(f1):
    S1 = open(f1, 'r')
    S1.seek(0,0)
    for line in S1.readlines():
    print("上次购买商品[商品名:数量]:".center(50,'-'))
    L=list(map(str, line.split(';')))
    # print(L)
    for i in range(len(L)):
    print (L[i])
    S1.close()
    else:
    print("目前还没有购买过任何物品")


    f2 = "C:\\Users\\admin\\Desktop\\shopping_Dict_DATE_%s.txt" % name_input
    if os.path.exists(f2):
    S2 = open(f2, 'r')
    S2.seek(0,0)
    for line in S2.readlines():
    print("上次购买商品时间".center(50,'-'))
    L=list(map(str, line.split(';')))
    # print(L)
    for i in range(len(L)):
    print (L[i])
    S2.close()

    f3 = "C:\\Users\\admin\\Desktop\\shopping_Dict_balance_%s.txt" % name_input
    if os.path.exists(f3):
    S3 = open(f3, 'r')
    S3.seek(0,0)
    for line in S3.readlines():
    print("上次购买商品后卡上余额".center(50,'-'))
    L=list(map(str, line.split(';')))
    # print(L)
    for i in range(len(L)):
    print (L[i])
    S3.close()
  • 相关阅读:
    JAVA集合框架01
    java基础===>点餐系统
    java基础===>数组的应用
    java基础===>双重循环打印图形
    java基础 ===》循环结构
    JAVA基础==>witch的应用!
    选择结构!
    路由系统
    flask使用及返回值、配置文件的四种方式
    短信验证码操作
  • 原文地址:https://www.cnblogs.com/feiyu_Team/p/5984426.html
Copyright © 2011-2022 走看看