Python编程 基础练习(四)

前端之家收集整理的这篇文章主要介绍了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,'black')
drawCircle(150,'red')
drawCircle(120,-60,'green')
drawCircle(50,'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

猜你在找的Python相关文章