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:
