




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、第二章習(xí)題解答2-4>>> num = input("please input a number: ")please input a number: >>> num = int(input("please input a number: ")please input a number: 2502-5(a)>>> i=0>>> while i<11:print(i)i+=101.(b)>>> i=0>>> for i in range(11):
2、print(i)i+=101.2-6>>> num=int(input("please input a num:")please input a num:366>>> if num<0:print('fushu')elif num>0:print('zheshu')else:print('zero')zheshu2-7>>> s=input('enter a string:')enter a string:Field>>> fo
3、r eachchar in s:print(eachchar)Field>>> for i in range(len(s):print(i,si)0 F1 i2 e3 l4 d>>> for i,x in enumerate(s):print(i,x)0 F1 i2 e3 l4 d2-8>>> r=0>>> for i in range(5):r+=int(input('enter a num:')print(r)enter a num:44enter a num:48enter a num:5563ent
4、er a num:555618enter a num:44662# uses sum() BIF and generator expressions>>> print(sum(int(input('please input a num:') for i in range(5)please input a num:2please input a num:2please input a num:2please input a num:2please input a num:210>>> 2-10>>> while True:nu
5、m=int(input("please input a num:(1-100)")if num in range(101):print('corect')breakelse:print('error,try again!')please input a num:(1-100)122error,try again!please input a num:(1-100)1corect2-15>>> num1 = int(input("please input first number:")please inpu
6、t first number:12>>> num2 = int(input("please input 2rd number:")please input 2rd number:5>>> num3 = int(input("please input 3st number:")please input 3st number:55>>> if num1 > num2:num1, num2 = num2, num1>>> if num1 > num3:num1, num3 =
7、num3, num1>>> if num1 > num3:num1, num3 = num3, num1>>> print(num1,num2,num3)5 12 55>>> print(num3,num2,num1)55 12 5>>> makeTextFile.py#!/usr/bin/env/ pythonprint('jhjjjjj')print('DONE'),附:第三四章例題執(zhí)行結(jié)果及源碼#例3.1#!/usr/bin/env python'makeTextFile
8、.py - create text file'import os# get filenamewhile True: fname = input('Enter file name: ') if os.path.exists(fname):#os.path.exists()函數(shù):確認(rèn)輸入的文件名是否存在 print("* ERROR: '%s' already exists" % fname) else: break# get file content (text) linesall = print("nEnter lines
9、('.' by itself to quit).n")# loop until user terminates inputwhile True: entry = input('> ') if entry = '.': break else: all.append(entry)# write lines to file with NEWLINE line terminatorfobj = open(fname, 'w') #打開文件準(zhǔn)備進(jìn)行寫操作fobj.write('n'.join(all) #將內(nèi)
10、存中內(nèi)容逐行寫入文件fobj.close() #print( 'DONE!') '''#例3.2#!/usr/bin/env python'readTextFile.py - read and display text file'# get filenamefname = input('Enter file name: ')print()# attempt to open file for readingtry: fobj = open(fname, 'r')except IOError as e: #py
11、3修改,舊版本為except IOError, e: print("* file open error:", e)else: # display contents to the screen for eachLine in fobj: print( eachLine), fobj.close()#例4.1 def displayNumType(num): print(num, 'is',) if isinstance(num, (int, float, complex):#isinstance()判斷對(duì)象類型 print('a number of type:', type(num)._name_) else: print('not a number at all!')displayNumType(-69)displayNumType(9999999999
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年物位儀項(xiàng)目發(fā)展計(jì)劃
- 稅務(wù)籌劃與合規(guī)管理策略計(jì)劃
- 2025年磁共振成像裝置項(xiàng)目發(fā)展計(jì)劃
- 2025年房屋整體質(zhì)量無損檢測(cè)分析系統(tǒng)項(xiàng)目建議書
- 文化藝術(shù)品交易免責(zé)協(xié)議書
- 宿舍舍長述職報(bào)告
- 貨物鐵路運(yùn)輸合同
- Tectoquinone-Standard-生命科學(xué)試劑-MCE
- O-2545-hydrochloride-生命科學(xué)試劑-MCE
- 【基于杜邦分析的雅戈?duì)栍芰Ψ治?900字】
- 橋面系小型構(gòu)件遮板等預(yù)制場(chǎng)施工方案
- 高二語文早讀材料積累(1-20周)課件159張
- 2023版教科版二年級(jí)下冊(cè)科學(xué)課堂作業(yè)本參考答案
- 規(guī)劃收費(fèi)標(biāo)準(zhǔn)
- 大連理工大學(xué)信封紙
- 讀《教師成長力-專業(yè)成長圖譜》有感
- 自動(dòng)化儀表工程施工及質(zhì)量驗(yàn)收規(guī)范
- 邵陽市職工勞動(dòng)能力鑒定表
- 中華人民共和國環(huán)境保護(hù)法
- 胎膜早破的護(hù)理PPT
評(píng)論
0/150
提交評(píng)論