操作題:
1,接收用戶輸入的一個浮點數,輸出這個浮點數的小數部分各字符的和,以10為寬度,靠右顯示,,采用星號*填充。
代碼:
s = input("請輸入一個小數:")
s = s[::1]
cs = ()
for c in s:if c = ",":breakcs += eval(c)
print("{:*>10}".format(cs))
2,time庫是Python語言中與時間處理相關的標準庫,time庫中ctime(函數能夠將-個表示時間的浮點數變成人類可以理解的時間格式,示例如下:
import time
print(ime. ctime(1519181231.0))
輸出結果是: Wed Feb 21 10:47:11 2018
代碼:
import time
t = input("請輸入一個浮點數時間信息:")
s = time.ctime(eval(t))
ls = s.split()
print(ls[3],split(":")[0])
3,以26個小寫字母和0~9數字為基礎,以用戶輸入的數字為種子,隨機生成10個8位密碼,并將每個密碼在單獨一行打印輸出。
代碼:
import random
s = input("請輸入隨機種子:")
ls=[]
for i in range(26):ls.append(chr(ord('a')+i))
for i in range(10):ls.append (chr(ord('0')+i))random.seed(eval(s))
for i in range(10):for j in range(8):print (ls[random.randint(0,35)],end='')print()
4,利用random庫ourte庫, ,在屏幕上繪制3個黑色的正方形,正方形的左下角點坐標和正方形邊長由andt函數產生,參數在代碼中給出。效果如下圖所示。
?
代碼:
import turtle as t
import random as r
r.seed(1)
t.pensize (2)
for i in range(3):length = r.randint(20,80)x0 = r.randint(-100,100)y0 = r.randint(-100,100)t.penup()t.goto(x0,y0)for j in range(4) :t.fd(length)t.seth(90*(j+1))
t.done()
知識點:
1,數據模型通常由數據結構、數據操作及數據約束三部分組成。
2,min()函數返回參數中的最小值,對于字典類型使用字典的鍵來比較大小,返回值也為字典的鍵
3,randint(a,b)函數可以產生一個[a,b]之間的隨機整數; random()函數沒有參數,且只能產生在[0,1)范圍內的隨機浮點數。