zoukankan      html  css  js  c++  java
  • Python编程 基础练习(四)

    1. 使用time库,把系统的当前时间信息格式化输出

    import locale
    import time
    
    # 以格式2020年08月24日18时50分21秒输出
    # python time 'locale' codec can't encode character 'u5e74' in position 2: encoding error报错的解决方法
    locale.setlocale(locale.LC_CTYPE, 'chinese')
    
    t = time.localtime()
    print(time.strftime('%Y年%m月%d日 %H时%M分%S秒', t))
    

    运行结果如下:

    2020年08月24日18时54分17秒
    
    • 1

    2. 使用turtle库,画奥运五环

    import turtle
    
    p = turtle
    p.pensize(8)  # 画笔尺寸设置5
    
    
    def drawCircle(x, y, c='red'):
        p.pu()          # 抬起画笔
        p.goto(x, y)    # 绘制圆的起始位置
        p.pd()          # 放下画笔
        p.color(c)      # 绘制c色圆环
        p.circle(50, 360)  # 绘制圆:半径,角度
    
    
    drawCircle(0, 0, 'blue')
    drawCircle(80, 0, 'black')
    drawCircle(150, 0, 'red')
    drawCircle(120, -60, 'green')
    drawCircle(50, -60, 'yellow')
    
    p.done()
    

    运行效果如下:
    在这里插入图片描述

    3. 简单实现账目管理系统功能,包括创建一个账户、存钱、取钱、退出系统的功能

    class Bank():
    	users = []
    	def __init__(self):
    		# 创建该账户信息   属性:账号 密码 姓名 金额
    		users = []
    		self.__cardId = input('请输入账号:')
    		self.__pwd = input('请输入密码:')
    		self.__userName = input('请输入姓名:')
    		self.__banlance = eval(input('请输入该账号金额:'))
    		# 将账户信息以字典添加进列表
    		users.append({'账号': self.__cardId, '密码': self.__pwd, '姓名': self.__userName, '金额': self.__banlance})
    		print(users)
    
    	# 存钱
    	def cun(self):
    		flag = True
    		while flag:
    			cardId = input('输入账号:')
    			while True:
    				if cardId == self.__cardId:
    					curPwd = input('输入密码:')
    					if curPwd == self.__pwd:
    						money = eval(input('存入金额:'))
    						print('存钱成功')
    						self.__banlance = self.__banlance + money
    						print('存入:{}元  余额:{}元'.format(money, self.__banlance))
    						flag = False
    						break
    					else:
    						print('密码错误,请重新输入!')
    						continue
    				else:
    					print('账号错误,请检查后重新输入!')
    					break
    	# 取钱
    	def qu(self):
    		flag1, flage2 = True, True
    		while flag1:
    			cardId = input('输入账号:')
    			while flage2:
    				if cardId == self.__cardId:
    					curPwd = input('输入密码:')
    					if curPwd == self.__pwd:
    						while True:
    							money = eval(input('取出金额:'))
    							if money <= self.__banlance:
    								print('取钱成功')
    								self.__banlance = self.__banlance - money
    								print('取出:{}元  余额:{}元'.format(money, self.__banlance))
    								flag1, flage2 = False, False     # 外层循环也退出
    								break
    							else:
    								print('余额不足,请重新输入要取的金额!')
    								continue
    					else:
    						print('密码错误,请重新输入!')
    						continue
    				else:
    					print('账号错误,请检查后重新输入!')
    					break
    
    
    bk = Bank()
    print('=============== 创建账号成功 =================')
    print('---------------------------------------------------')
    
    while True:
    	print('1.存钱
    2.取钱
    3.退出账目管理')
    	order = input('请输入您的选择:')
    	if order == '1':
    		bk.cun()
    	elif order == '2':
    		bk.qu()
    	elif order == '3':
    		print('===================== 退出系统 =======================')
    		break
    	else:
    		print('输入选择有误,请重新输入!')
    		continue
    

    4. numpy数组操作

    • 创建一个 10x10 的随机数组,里面每个元素为 0-100 的整数,求它的最大值与平均值
    • 已知列表[[4,2,8,6],[7,5,9,1]],请将列表转换为ndarray对象,并将前2行的1、3列置为0,并重新输出
    import numpy as np
    
    s = np.random.randint(0, 100, size=(10, 10))   # 生成随机数组  里面每个元素为0-100的整数
    print(s)
    print(f'最大值:{np.max(s)}')
    print(f'平均值:{np.mean(s)}')
    s= np.array([[4, 2, 8, 1], [7, 5, 9, 6], [1, 2, 3, 4]])
    print(s)
    print('-------------------------')
    s[:2, 0:3:2] = 0    # 前两行的1、3列
    print(s)
    
    运行结果如下:
    [[59 18 93 34  6 59 72 26 27 98]
     [ 4 44 29 32 29 95 97 33 64 66]
     [64 64 31 26 61 35 65 92 31 46]
     [59 37 82 55 34 31 21 66 23 79]
     [29 58 73 30 34 94 17 49 63 60]
     [78 55 74 25  8 58 34 80 67 68]
     [76 49 57 86 23  2 76 36 35 95]
     [47 98 76 14 89 71 17 32 81 63]
     [70 76 77 10 98  6 78 35  2 69]
     [17  1 93 67 32 86 43  2 86 84]]
    最大值:98
    平均值:51.96
    [[4 2 8 1]
     [7 5 9 6]
     [1 2 3 4]]
    -------------------------
    [[0 2 0 1]
     [0 5 0 6]
     [1 2 3 4]]
    

    5. 蛇皮走位

    import turtle
    
    t = turtle
    t.penup()
    t.fd(-250)
    t.pendown()
    # 海龟腰围
    t.pensize(30)
    # 海龟颜色
    t.pencolor("purple")
    t.seth(-30)
    for i in range(5):   # 5节
        # 以左侧距离为r(如果r是负数,则以右侧距离-r)的点为圆心蛇皮走位(半径,旋转角度)
        t.circle(50, 2 * 30)
        t.circle(-50, 2 * 30)
    
    t.circle(50, 30)
    t.fd(60)               # 蛇身
    t.circle(25, 180)      # 蛇头的半径  角度
    t.fd(40*2 / 3)         # 蛇头长度
    
    t.done()          # 程序运行之后不会退出
    

    运行效果如下:

    在这里插入图片描述

    6. 文件操作

    下面是一个传感器采集数据文件sensor-data.txt的一部分。其中,每行是一条记录,逗号分隔多个属性。属性包括日期、时间、温度、湿度、光照、电压。其中,温度处于第3列。
    date,time,temp,humi,light,volt
    2020-02-01,23:03:16.33393,19.3024,38.4629,45.08,2.68742
    2020-02-01,23:06:16.01353,19.1652,38.8039,45.08,2.68742
    2020-02-01,23:06:46.77808,19.175,38.8379,45.08,2.68942
    请用读入文件的形式编写程序,统计并输出温度的平均值,结果保留2位小数。

    # 统计并输出温度的平均值,结果保留2位小数。
    with open(r'./测试数据/test_01.txt') as f:
        con = f.read().split('
    ')
    
    temp = [eval(x.split(',')[2]) for x in con[1:]]      # 第一行为列名  不要
    print('温度的平均值为:{:.2f}'.format(sum(temp) / len(temp)))
    

    运行结果如下:

    温度的平均值为:19.21

    本文首发于python黑洞网,博客园同步更新

     
  • 相关阅读:
    jQuery ui 利用 datepicker插件实现开始日期(minDate)和结束日期(maxDate)
    jQuery选择器大全
    Jquery插件 easyUI属性汇总
    JQuery EasyUI 对话框的使用方法
    jQuery Alert Dialogs (Alert, Confirm, & Prompt代替方案)
    JavaScript 开发者经常忽略或误用的七个基础知识点
    如何学习Javascript
    父子页面之间跨域通信的方法
    .NET中常见的内存泄露问题——GC、委托事件和弱引用
    启动外部exe程序
  • 原文地址:https://www.cnblogs.com/pythonzhilian/p/13728806.html
Copyright © 2011-2022 走看看