python核心编程怎么做_Python核心编程 练习

2–9.

循环和运算符 创建一个包含五个固定数值的列表或元组,输出他们的平均值。本练习的难点之一是通过除法得到平均值。 你会发现整数除会截去小数,因此你必须使用浮点除以得到更精确的结果。 float()内建函数可以帮助你实现这一功能。

>>> def a(): #function1

i=0

sum=0while i<5:

i=i+1num=input('enter %d:'%(i))

sum=sum+float(num)

avg=sum/5

print("avg is %f"%(avg))>>>a()

enter1:1enter2:2enter3:3enter4:4enter5:56avgis 13.200000

>>> def a(): #function2

print('input five number:')

i=0

sum=0while i<5:

i=i+1num=input()

sum=sum+float(num)

avg=sum/5

print("avg is %f"%(avg))>>>a()

input five number:3

4

67

23

564.34avgis 132.268000

2–10.

带循环和条件判断的用户输入 使用raw_input()函数来提示用户输入一个1 和100 之间的数,如果用户输入的数满足这个条件,显示成功并退出。否则显示一个错误信息然后再次提示用户输入数值,直到满足条件为止。

defa():

i=0while 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!")exceptValueError:print("please input number!")>>>a()

please enter a number between1 and 100:sad

please input number!

please enter a number between1 and 100:100number needs1~100!

please enter a number between1 and 100:23you entered the number23.000000

2–11.

带文本菜单的程序 写一个带文本菜单的程序,菜单项如下(1)取五个数的和 (2) 取五个数的平均值....(X)退出。由用户做一个选择,然后执行相应的功能。当用户选择退出时程序结束。这个程序的有用之处在于用户在功能之间切换不需要一遍一遍的重新启动你的脚本。(这对开发人员测试自己的程序也会大有用处)

deffunction():print('input 5 numbers:')

i=0

sum=0while i<5:

i=i+1num=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、怎么修改完整的文件,包括删除语句、修改原有语句

defb():#!/usr/bin/env python

'makeTextFile.py--creat text file'

importos

ls=os.linesep#get filename

whileTrue: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:whileTrue:

fname=input('enter a filename:')ifos.path.exists(fname):print("Error:'%s' already exists"%(fname))else:break

#get file content (text) lines

allw=[]print("\n Enter lines('.' by itself to quit).\n")#loop until user terminates input

whileTrue:

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 inallw])

fobjw.close()print('Seccess new!')if select==2:whileTrue:

fname=input('enter a filename:')ifos.path.exists(fname):

fobjr=open(fname,'r')for filetxt infobjr:print(filetxt)

fobjr.close()break

else:print('%s file is not exist'%(fname))print('Seccess read!')if select==3:whileTrue:

fname=input('enter a filename:')ifos.path.exists(fname):break

else:print('%s file is not exist'%(fname))

fobjr=open(fname,'r')

all=fobjr.readlines()print("\n Enter lines('.' by itself to quit).\n")whileTrue:

entry=input('+')if entry=='.':break

else:all.append(entry)

fobj=open(fname,'w')

fobj.writelines(['%s%s'%(x,ls)for x inall])

fobj.close()

fobjr.close()print("Seccess modify")if select==4:break

5-4 取余。判断给定年份是否是闰年。使用下面的公式:一个闰年就是指它可以被4 整除,但不能被100 整除, 或者它既可以被4 又可以被100 整除。比如 1992,1996 和2000 年是闰年,但1967 和1900 则不是闰年。下一个是闰年的整世纪是 2400 年。

defLeap():

x=int(input('enter;'))

a=x%4b=x%100c=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')

View Code

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 美分这样的结果都是不符合要求的。

defyuan():

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=felse:

n3=e

n4=felse:

n2=c

(e,f)=divmod(d,5)if e==0:

n3=0

n4=felse:

n3=e

n4=felse:

n1=a

(c,d)=divmod(b,10)if c==0:

n2=0

(e,f)=divmod(b,5)if e==0:

n3=0

n4=felse:

n3=e

n4=felse:

n2=c

(e,f)=divmod(d,5)if e==0:

n3=0

n4=felse:

n3=e

n4=fprint(x,'equals %d $25 %d $10 %d $5 %d $1'%(n1,n2,n3,n4))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值