習(xí)題答案

作業(yè)四按要求編寫程序(任選三題)

1.編寫一個(gè)從 1 加到 end 的當(dāng)型循環(huán)擅编。變量 end 的值由鍵盤輸入。假如輸入end

的值為6硼补,則代碼輸出的結(jié)果應(yīng)該是21奋刽,也就是1+2+3+4+5+6 的結(jié)果(不要用

sum作為變量,因?yàn)樗莾?nèi)置函數(shù))。

1. a = input()

2. b=int(a)

3. total = 0

4. for i in range(b+1):

5.? ? ? total = i+total

6. print(total)


2、從鍵盤輸入一個(gè)整數(shù),判斷該數(shù)字能否被 2 和 3 同時(shí)整除沮趣,能否被 2 整除,能否被 3 整除坷随,不能被 2 和 3 整除房铭。輸出相應(yīng)信息。


a = input()

b=int(a)

if b%2==0 and b%3==0:

??? print('該數(shù)字能被 2 和 3 同時(shí)整除')

elif b%2==0:

??? print('該數(shù)字能被 2 整除')

elif b%3==0:

??? print('該數(shù)字能被 3 整除')

else :

? ? ?print('該數(shù)字不能被 2 和 3 整除')


3温眉、 一個(gè)數(shù)如果恰好等于它的因子之和缸匪,這個(gè)數(shù)就稱為“完數(shù)”,例如类溢, 6 的因子為1凌蔬、2、3闯冷,而6=1+2+3砂心,因此6 是“完數(shù)”。編程序找出 1000 之內(nèi)的所有完數(shù)蛇耀,并按下面的格式輸出其因子:6its factors are 1, 2, 3

??? for iin range(1001): if i==0: continue s = 0 for j in range(i): if j==0: continue ifi%j==0: s=s+j if s==i: print('{} its factors are '.format(i),end='') for j inrange(i): if j == 0: continue if i % j == 0: print(j,end=' ') print('\n')



4辩诞、打印出所有的“水仙花數(shù)”,所謂“水仙花數(shù)”是指一個(gè)?3?位數(shù)蒂窒,其各位數(shù)字立方和等于該數(shù)本身躁倒。例如,?153?是一個(gè)水仙花數(shù)洒琢,因?yàn)?153=13+53+33。

???for i in range(1000):

???if i<100:

??????? continue

???s = 0

???a = int(i//100)

???b = int(i//10%10)

???c = int(i%10)

???s = a**3+b**3+c**3

???if s == i:

??????? print("{}是水仙花數(shù)".format(i))


5.假設(shè)x褐桌,y是(-1,1)中的兩個(gè)隨機(jī)數(shù)字(服從正態(tài)分布):

(1)當(dāng)x2+y2>1時(shí)衰抑,返回x,y的值

(2)創(chuàng)建一個(gè)列表荧嵌,其中每個(gè)元素對(duì)應(yīng)為\sqrt{x*ln(a)} 的值,?且 a=x2+y2.

import random

import math

def randomXY():

? ? x= random.uniform(-1,1)

? ? y= random.uniform(-1,1)

if(x*x+y*y>1):

? return randomXY()

else:

? ? return (x,y)

def ans():

list = []

for i in range(0,100):

? ? xy = randomXY()

? ? x = xy[0]

? ? y = xy[1]

? ? a = x*x+y*y

? ? temp = math.sqtr(x*math.log(a,math.e))

? ? list.append(temp)

return list

# print(ans())

for x in ans():

print(x)


作業(yè)五

第四題選作

一呛踊、 輸出 1~100 之間不能被 7 整除的數(shù),每行輸出 10 個(gè)數(shù)字啦撮,要求應(yīng)用字符串格式化方法(任何一種均可) 美化輸出格式谭网。 輸出效果為:

j=0

for i in range(101):? #循環(huán)

??? if i>0 and i % 7 == 0: #如果能整除7,則再執(zhí)行循環(huán)

??????? continue

??? elif i>0:??? #如果大于0并且不能被7整除赃春,輸出愉择;

??????? print("{:3d}".format(i),end=' ')

??????? j += 1??#j用來控制每行輸出是否是10個(gè)

??????? if j % 10 == 0:

??????????? print('\n')

??????????? j = 0

? ??else:

? ? ? ? continue;


二、創(chuàng)建文本文件FarewellCambridge.txt。內(nèi)容為:

Very quietly I take my leave

As quietly as I came here;

Quietly I wave good-bye

To the rosy clouds in the western sky.

The golden willows by the riverside

Are young brides in the setting sun;

Their reflections on the shimmering waves

Always linger in the depth of my heart.

The floating heart growing in the sludge

Sways leisurely under the water;

In the gentle waves of Cambridge

I would be a water plant!

That pool under the shade of elm trees

Holds not water but the rainbow from thesky;

Shattered to pieces among the duckweeds

Is the sediment of a rainbow-like dream?

To seek a dream? Just to pole a boatupstream

To where the green grass is more verdant;

Or to have the boat fully loaded withstarlight

And sing aloud in the splendor ofstarlight.

But I cannot sing aloud

Quietness is my farewell music;

Even summer insects heap silence for me

Silent is Cambridge tonight!

并用寫字板查看(注意:直接將上述詩句拷貝到程序中锥涕,不需要自己再次錄入)衷戈。


file =open('C:/Users/uestc/Desktop/hanzhengqiang.txt','w')

#在桌面新建一個(gè)韓正強(qiáng).txt文本文檔,并且是寫的模式

#在文本中寫入文字:

file.write('''Very quietly I take my leave

As quietly as I came here;

Quietly I wave good-bye

To the rosy clouds in the western sky.

The golden willows by the riverside

Are young brides in the setting sun;

Their reflections on the shimmering waves

Always linger in the depth of my heart.

The floating heart growing in the sludge

Sways leisurely under the water;

In the gentle waves of Cambridge

I would be a water plant!

That pool under the shade of elm trees

Holds not water but the rainbow from the sky;

Shattered to pieces among the duckweeds

Is the sediment of a rainbow-like dream?

To seek a dream? Just to pole a boat upstream

To where the green grass is more verdant;

Or to have the boat fully loaded with starlight

And sing aloud in the splendor of starlight.

But I cannot sing aloud

Quietness is my farewell music;

Even summer insects heap silence for me

Silent is Cambridge tonight!''')



?對(duì)于上一題建立的文本文件层坠,使用read()讀文件殖妇,并在屏幕上顯示。

1.? file = open('C:/Users/uestc/Desktop/hanzhengqiang.txt','r')

2.? #在桌面打開一個(gè)韓正強(qiáng).txt文本文檔破花,并且是讀的模式

3.?#讀取文件谦趣,并且輸出:

4.? s=file.read()

5.? print(s)


根據(jù)給定的文本文件words.txt(可將該文件存放在任意目錄,注意打開文件時(shí)要加入正確的路徑)編寫函數(shù)loadWords()座每,words.txt包含若干小寫英文單詞前鹅。要求:

1) 讀入該文件,統(tǒng)計(jì)并輸出單詞的個(gè)數(shù)

2) 返回單詞列表(注意:指返回單詞列表即可尺栖,不要在屏幕上顯示)


import re

#統(tǒng)計(jì)單詞個(gè)數(shù):

def count_words(file_path):

??? with open(file_path) as file:

??????? text = file.read()

??????? words = re.findall(r'[a-zA-Z]+', text)

??????? count = len(words)

??????? return count

print(count_words('C:/Users/uestc/Desktop/words.txt'))




作業(yè)七

一嫡纠、編寫函數(shù)devide(x, y),x為被除數(shù)延赌,y為除數(shù)除盏。要求考慮異常情況的處理。

1挫以、被零除時(shí)者蠕,輸出"division by zero! ";

2掐松、類型不一致時(shí)踱侣,強(qiáng)制轉(zhuǎn)換為整數(shù)再調(diào)用本函數(shù);

3大磺、 若沒有上述異常則輸出計(jì)算結(jié)果抡句。


def test(password, earning, age):

???#assert 1<0

???assert password[0] not in ['0','1','2','3','4','5','6','7','8','9']

???assert int(earning)>=0 and int(earning)<=20000

???assert int(age)>=18 and int(age)<=70

???return True

print(test('as','11','30'))



二、?編寫函數(shù)test(password,earning, age)用于檢測輸入錯(cuò)誤杠愧。要求輸入密碼password第一個(gè)符號(hào)不能是數(shù)字待榔,工資earnings的范圍是0—20000,工作年齡的范圍是18—70流济。使用斷言來實(shí)現(xiàn)檢查锐锣,若三項(xiàng)檢查都通過則返回True。


def test(password, earning, age):

??? #assert 1<0

??? assert password[0] not in['0','1','2','3','4','5','6','7','8','9']

??? assert int(earning)>=0 andint(earning)<=20000

??? assert int(age)>=18 and int(age)<=70

? ?return True


三绳瘟、使用tkinter庫完成下列程序

1雕憔、編程實(shí)現(xiàn)如下GUI(只顯示,沒有回調(diào)函數(shù))

import tkinter

root =tkinter.Tk()

root.title('tk')

theLabel=tkinter.Label(root,text='Welcome to Python Tkinter')

theLabel.pack()

root.geometry('500x300+300+300')

button = tkinter.Button(root,text='Click Me')

button.pack()

root.mainloop()


2糖声、?編程響應(yīng)用戶事件斤彼》质荩“OK”按鈕前景為紅色,按下“OK”按鈕顯示"OK button is clicked"畅卓,”Cancel“按鈕背景為黃色擅腰,按下”Cancel“按鈕顯示"Cancel button is clicked"。

import tkinter

def ok_on_click():

??? print('OK button is clicked')

def cancel_on_click():

??? print('Cancel button is clicked')

root = tkinter.Tk()

root.title('zy')

label = tkinter.Label(root)

buttonok = tkinter.Button(root,text='OK',fg='red')

buttonok['command']=ok_on_click

label.pack()

buttonok.pack()

root.geometry('500x300+300+300')

buttoncancel =tkinter.Button(root,text='Cancel',bg='yellow')

buttoncancel['command']=cancel_on_click

label.pack()

buttoncancel.pack()

root.mainloop()


四翁潘、?使用Turtle庫完成下列程序

1趁冈、繪制一個(gè)紅色的五角星圖形,如圖所示拜马。

import? turtle as tk

tk.fillcolor("red")

tk.begin_fill()

while True:

??? tk.forward(200)

??? tk.right(144)

??? if abs(tk.pos()) < 1:

??????????? break

tk.end_fill()

input()


2渗勘、螺旋線繪制(每一圈是四個(gè)線段組成,一共繪制100個(gè)線段)俩莽。繪制一個(gè)螺旋線的圖形旺坠,如圖所示。

1. importturtle

2. importtime

3.turtle.pensize(1)

4. for x in range(100):

5.? turtle.forward(x)

6.? turtle.left(90)

7. time.sleep(100)



作業(yè)十

一扮超、編寫一個(gè)程序取刃,以2位數(shù)字,X,Y作為輸入出刷,生成一個(gè)二維數(shù)組璧疗。數(shù)組的第i行和第j列中的元素值應(yīng)該是i*j。

注意:i= 0,1 . .,X - 1; ? ?j = 0,1,--Y-1馁龟。

例子假設(shè)程序有以下輸入:3崩侠、5

則程序輸出為:[[0,0,0,0,0],[0,1,2,3,4],[0,2,4,6,8]]

提示:注意:如果要為問題提供輸入數(shù)據(jù),應(yīng)該假設(shè)它是一個(gè)控制臺(tái)輸入坷檩,以逗號(hào)分隔却音。


print('請(qǐng)輸入兩個(gè)數(shù)字:')

input_str =input()

dimensions=[int(x) for x in input_str.split(',')]

rowNum=dimensions[0]

colNum=dimensions[1]

multilist = [[0 for col in range(colNum)] for row in range(rowNum)]

for row in range(rowNum):

??? for col in range(colNum):

??????? multilist[row][col]= row*col

print (multilist)





Python作業(yè) | Zander`s blog (zanderwang.com)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市矢炼,隨后出現(xiàn)的幾起案子系瓢,更是在濱河造成了極大的恐慌,老刑警劉巖句灌,帶你破解...
    沈念sama閱讀 222,946評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件八拱,死亡現(xiàn)場離奇詭異,居然都是意外死亡涯塔,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門清蚀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來匕荸,“玉大人,你說我怎么就攤上這事枷邪¢簧Γ” “怎么了诺凡?”我有些...
    開封第一講書人閱讀 169,716評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長践惑。 經(jīng)常有香客問我腹泌,道長,這世上最難降的妖魔是什么尔觉? 我笑而不...
    開封第一講書人閱讀 60,222評(píng)論 1 300
  • 正文 為了忘掉前任凉袱,我火速辦了婚禮,結(jié)果婚禮上侦铜,老公的妹妹穿的比我還像新娘专甩。我一直安慰自己,他們只是感情好钉稍,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評(píng)論 6 398
  • 文/花漫 我一把揭開白布涤躲。 她就那樣靜靜地躺著,像睡著了一般贡未。 火紅的嫁衣襯著肌膚如雪种樱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,807評(píng)論 1 314
  • 那天俊卤,我揣著相機(jī)與錄音嫩挤,去河邊找鬼。 笑死瘾蛋,一個(gè)胖子當(dāng)著我的面吹牛俐镐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播哺哼,決...
    沈念sama閱讀 41,235評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼佩抹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了取董?” 一聲冷哼從身側(cè)響起棍苹,我...
    開封第一講書人閱讀 40,189評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎茵汰,沒想到半個(gè)月后枢里,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,712評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蹂午,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評(píng)論 3 343
  • 正文 我和宋清朗相戀三年栏豺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片豆胸。...
    茶點(diǎn)故事閱讀 40,926評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奥洼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出晚胡,到底是詐尸還是另有隱情灵奖,我是刑警寧澤嚼沿,帶...
    沈念sama閱讀 36,580評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站瓷患,受9級(jí)特大地震影響骡尽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜擅编,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評(píng)論 3 336
  • 文/蒙蒙 一攀细、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧沙咏,春花似錦辨图、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至吆豹,卻和暖如春鱼的,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背痘煤。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評(píng)論 1 274
  • 我被黑心中介騙來泰國打工凑阶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人衷快。 一個(gè)月前我還...
    沈念sama閱讀 49,368評(píng)論 3 379
  • 正文 我出身青樓宙橱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蘸拔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子师郑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • 塞浦路斯護(hù)照,入籍塞浦路斯调窍,塞浦路斯永居宝冕,辦理塞浦路斯移民、快速入籍 塞浦路斯護(hù)照邓萨、居留地梨,保加利亞護(hù)照、居留缔恳,馬耳...
    Frank來閱讀 158評(píng)論 0 0
  • 人要有點(diǎn)自己的愛好宝剖,最好是高雅的,可以陶冶情操的歉甚,可以琴棋書畫诈闺,也可以詩酒茶。 最近家里的小女迷上了畫畫铃芦,動(dòng)漫人物...
    林子夕的小屋閱讀 168評(píng)論 0 0
  • 《腦力賦能》讀書筆記 我們終生忙碌雅镊,長時(shí)間工作,很難有時(shí)間顧及自己刃滓,因此健康受到損害仁烹。同時(shí),我們也很少有時(shí)間顧及別...
    小北bling閱讀 1,170評(píng)論 0 0
  • 本周自學(xué)活法咧虎,參與新事業(yè)的動(dòng)機(jī)每晚自問自答卓缰,和為社會(huì)為世人勇于自我犧牲兩個(gè)小結(jié),講述了稻盛和夫老先生為國民做貢獻(xiàn)的...
    張偉99閱讀 242評(píng)論 0 0
  • 【2】 海子的溫情在這首詩中間變成了一種對(duì)于人們的大愛砰诵。這份愛是從哪里來的征唬?或許是記憶中的外套那種沉甸甸的時(shí)光感,...
    quiteafew閱讀 115評(píng)論 0 0