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秒
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()
執行效果如下:
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.存錢\n2.取錢\n3.退出賬目管理')
order = input('請輸入您的選擇:')
if order == '1':
bk.cun()
elif order == '2':
bk.qu()
elif order == '3':
print('===================== 退出系統 =======================')
break
else:
print('輸入選擇有誤,請重新輸入!')
continue
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]]
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() # 程式執行之後不會退出
執行效果如下:
下面是一個感測器採集資料檔案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('\n')
temp = [eval(x.split(',')[2]) for x in con[1:]] # 第一行為列名 不要
print('溫度的平均值為:{:.2f}'.format(sum(temp) / len(temp)))
執行結果如下:
溫度的平均值為:19.21