2–9.
循环和运算符 创建一个包含五个固定数值的列表或元组,输出他们的平均值。本练习的难点之一是通过除法得到平均值。 你会发现整数除会截去小数,因此你必须使用浮点除以得到更精确的结果。 float()内建函数可以帮助你实现这一功能。
>>> def a(): #function1 i=0 sum=0 while i<5: i=i+1 num=input('enter %d:'%(i)) sum=sum+float(num) avg=sum/5 print("avg is %f"%(avg)) >>> a() enter 1:1 enter 2:2 enter 3:3 enter 4:4 enter 5:56 avg is 13.200000
>>> def a(): #function2 print('input five number:') i=0 sum=0 while i<5: i=i+1 num=input() sum=sum+float(num) avg=sum/5 print("avg is %f"%(avg)) >>> a() input five number: 3 4 67 23 564.34 avg is 132.268000
2–10.
带循环和条件判断的用户输入 使用raw_input()函数来提示用户输入一个1 和100 之间的数,如果用户输入的数满足这个条件,显示成功并退出。否则显示一个错误信息然后再次提示用户输入数值,直到满足条件为止。
def a(): i=0 while i==0: try: a=input('please enter a number between 1 and 100:') num=float(a) if num>1 and num<100: print('you entered the number %f'%(num)) break else: print("number needs 1~100!") except ValueError: print("please input number!") >>> a() please enter a number between 1 and 100:sad please input number! please enter a number between 1 and 100:100 number needs 1~100! please enter a number between 1 and 100:23 you entered the number 23.000000
2–11.
带文本菜单的程序 写一个带文本菜单的程序,菜单项如下(1)取五个数的和 (2) 取五个数的平均值....(X)退出。由用户做一个选择,然后执行相应的功能。当用户选择退出时程序结束。这个程序的有用之处在于用户在功能之间切换不需要一遍一遍的重新启动你的脚本。(这对开发人员测试自己的程序也会大有用处)
def function(): print('input 5 numbers:') i=0 sum=0 while i<5: i=i+1 num=input() sum+=float(num) while 1: print('1.sum') #这几个print可以放到while 1外面去,表示不需要每次选择前都会显示选项 print('2.average') print('3.exit') select=int(input('you select:')) if select==1: print('the sum is:'), print(sum) elif select==2: avg=sum/5 print('the average is:'), print(avg) else:break
3-13
这个题目实在是太折腾了,将新建文本、查看文本、新增文本内容写到一起,然后带文本菜单。
不过还是搞不清楚两点:1、怎么删掉多出来的空格;2、怎么修改完整的文件,包括删除语句、修改原有语句
def b(): #!/usr/bin/env python 'makeTextFile.py--creat text file' import os ls=os.linesep #get filename while True: print("1.creat a file");print("2.read a file");print("3.modify a file");print("4.exit") select=int(input('I want:')) if select==1: while True: fname=input('enter a filename:') if os.path.exists(fname): print("Error:'%s' already exists"%(fname)) else:break #get file content (text) lines allw=[] print(" Enter lines('.' by itself to quit). ")#loop until user terminates input while True: entry=input('>') if entry=='.': break else: allw.append(entry) #write lines to file with proper line-ending fobjw=open(fname,'w') fobjw.writelines(['%s%s'%(x,ls)for x in allw]) fobjw.close() print('Seccess new!') if select==2: while True: fname=input('enter a filename:') if os.path.exists(fname): fobjr=open(fname,'r') for filetxt in fobjr: print(filetxt) fobjr.close() break else: print('%s file is not exist'%(fname)) print('Seccess read!') if select==3: while True: fname=input('enter a filename:') if os.path.exists(fname): break else: print('%s file is not exist'%(fname)) fobjr=open(fname,'r') all=fobjr.readlines() print(" Enter lines('.' by itself to quit). ") while True: entry=input('+') if entry=='.': break else:all.append(entry) fobj=open(fname,'w') fobj.writelines(['%s%s'%(x,ls)for x in all]) fobj.close() fobjr.close() print("Seccess modify") if select==4:break
5-4 取余。判断给定年份是否是闰年。使用下面的公式:一个闰年就是指它可以被4 整除,但不能被100 整除, 或者它既可以被4 又可以被100 整除。比如 1992,1996 和2000 年是闰年,但1967 和1900 则不是闰年。下一个是闰年的整世纪是 2400 年。

def Leap(): x=int(input('enter;')) a=x%4 b=x%100 c=x%400 if c==0: m='is' elif a==0: if b==0: m='is not' else: m='is' else: m='is not' print('%d'%x, m,'Leap Year')
5-5 取余。取一个任意小于1 美元的金额,然后计算可以换成最少多少枚硬币。硬币有1美分,5 美分,10 美分,25 美分四种。1 美元等于100 美分。举例来说,0.76 美元换算结果应该是 3 枚25 美分,1 枚1 美分。类似76 枚1 美分,2 枚25 美分+2 枚10 美分+1 枚5 美分+1枚1 美分这样的结果都是不符合要求的。
def yuan(): x=float(input('enter a number of dollor:')) z=100*x (a,b)=divmod(z,25) if a==0: n1=0 (c,d)=divmod(z,10) if c==0: n2=0 (e,f)=divmod(z,5) if e==0: n3=0 n4=f else: n3=e n4=f else: n2=c (e,f)=divmod(d,5) if e==0: n3=0 n4=f else: n3=e n4=f else: n1=a (c,d)=divmod(b,10) if c==0: n2=0 (e,f)=divmod(b,5) if e==0: n3=0 n4=f else: n3=e n4=f else: n2=c (e,f)=divmod(d,5) if e==0: n3=0 n4=f else: n3=e n4=f print(x,'equals %d $25 %d $10 %d $5 %d $1'%(n1,n2,n3,n4))