第4章无编程题故略过。
5-2.操作符。
(a)写一个函数,计算并返回两个数的乘积。
(b)写一段代码调用这个函数,并显示它的结果。
def mutiple(x, y): return x*y if __name__=="__main__": a = int(raw_input("input a number: ")) b = int(raw_input("input a number: ")) print mutiple(a,b)
5-3.标准类型操作符。写一段脚本,输入一个测验成绩,根据下面的标准,输出他的评分成绩(A-F)。
A:90~100 B:80~89 C:70~79 D:60~69 F:<60
while 1: number=int(raw_input('enter a number between 1 and 100: ')) if number<60: print "F" elif 60<=number<=69: print "D" elif 70<=number<=79: print "C" elif 80<=number<=89: print "B" elif 90<=number<=100: print "A" else: print "wrong number,please input again" break
while 1: year=int(raw_input('enter a year between 1000 and 9999: ')) if year<1000 or year>9999: break elif (year%4==0 and year%100!=0) or (year%4==0 and year%100==0): print "leap year" else: print "not leap year"
while 1: coin=int(raw_input('enter a coin between 1 and 100: ')) if coin<1 or coin>99: break n25=coin/25 coin=coin-25*n25 n10=coin/10 coin=coin-10*n10 n5=coin/5 coin=coin-5*n5 n1=coin print '25 cent: %d \n10 cent: %d \n5 cent: %d \n1 cent: %d'%(n25,n10,n5,n1)
while 1: print "number1 (+|-|*|/|%|**) number2" expression=raw_input('please input a expression: ') if '+' in expression: a,b=expression.split('+',1) print float(a)+float(b) elif '-' in expression: a,b=expression.split('-',1) print float(a)-float(b) elif '**' in expression: a,b=expression.split('**',1) print float(a)**float(b) elif '/' in expression: a,b=expression.split('/',1) print float(a)/float(b) elif '%' in expression: a,b=expression.split('%',1) print float(a)%float(b) elif '*' in expression: a,b=expression.split('*',1) print float(a)*float(b) else: print "wrong expression" break
price=float(raw_input('please input a earn price: ')) print 'your tax is %10.2f' %round(price*0.2,2)
num=float(raw_input('please input a real number: ')) print 'the area of square is %f' %(num*num) print 'the area of circle is %f' %(math.pi*num*num) print 'the cubage of cube is %f' %(num**3) print 'the cubage of sphere is %f' %(math.pi*(num**3)*3/4.0)
def F2C(tem): Fahrenheit=float(tem) Celsius=(Fahrenheit-32)*(5/9.0) print '%fF equals to %fC°' %(Fahrenheit,Celsius)
for num in range(0,21): if num%2==0: print '%d is even' %num else: print '%d is odd' %num def divide(a,b): if a==0 and b==0: print 'wrong input number' return elif b==0: a,b=b,a if a<b and a!=0: if b%a==0: print 'true' else: print 'false' else: if a%b==0: print 'true' else: print 'false'
print sys.maxint print -sys.maxint-1 print sys.float_info print sys.long_info
5-13 转换。写一个函数把由小时和分钟表示的时间转换为只用分钟表示的时间。
print "time format as HH:MM" timestr=raw_input('please input a time: ') def totalmin(time): HH,MM=time.split(':',1) print '%d total minutes' %(int(HH)*60+int(MM)) totalmin(timestr)
复利: 最终金额=本金*(1+第1期报酬率)*(1+第2期报酬率)*(1+第3期报酬率)* ….. *(1+第N期报酬率)
年回报率 = (1+第1期报酬率)*(1+第2期报酬率)*(1+第3期报酬率)* ….. *(1+第N期报酬率)
dayrate=float(raw_input("input a rate ")) print 'a year rate of earn is %f' %((1+dayrate)**365-1)
最大公约数,用相除法,求得余数,直到一个数为0,循环结束
最小公倍数就是两个数的乘积除以最大公约数
def gcd(a,b): while(a>0): c=int(a) a=int(b)%a b=c print 'gcd is %d' %b def lcm(a,b): return int(a)*int(b)/gcd(a,b)
balance=float(raw_input("Enter opening balance: ")) paid=float(raw_input("Enter monthly payment: ")) def payment(balance,paid): print ' Amount Remaining' print 'Pymt# Paid Balance' print '----- ----- ------' b=balance m=int(b/paid) b=b-m*paid while(b>0): m+=1 b=b-paid print '%d%11.2f%8.2f' %(0,0.00,balance) for month in range(1,m): print '%d%11.2f%8.2f' %(month,paid,balance-month*paid) print '%d%11.2f%8.2f' % (m,(balance-paid*(m-1)),0) payment(balance,paid)
while 1: N1=random.randint(2,100) N2=random.randint(1,100) if N1>N2: break print 'N1 is %d, N2 is %d' %(N1,N2) list1=[] for i in range(0,N1): n=random.randint(0,2**31-1) list1.append(n) print list1 list2=random.sample(list1,N2) list2.sort() print list2