初学Python-第五章练习题

来源:互联网 发布:芜湖 网络推广专员 编辑:程序博客网 时间:2024/05/17 13:13

5-2.操作符
(a)写一个函数,计算并返回两个数的成绩。

def multiply(num1,num2):    sum1 = num1 * num2    print sum1

(b)写一段代码调用这个函数,并显示它的结果。

#!/usr/bin/env pythonimport randomdef multiply(num1,num2):        sum1 = num1 * num2        return sum1a = random.randint(1,10)b = random.randint(1,10)sum2 = multiply(a,b)print sum2

5-3根据测试成绩判断等级。

#!/usr/bin/pythonwhile True:        socre = raw_input('Enter your socre(1-100): ')        if socre >= 90 and socre <= 100:                print "Your scores is A"        elif socre >= 80 and socre<=89:                print "Your scores is B"        elif socre >= 70 and socre<=79:                print "Your scores is C"        elif socre >= 60 and socre<=69:                print "Your scores is D"        else:                print "Your scores is F"

5-4闰年计算
除去世纪年需要整除400,其余可以整除4的是闰年。和书上说的世纪年即可以整除4也可以整除100有歧义,是我理解的有问题?我觉得应该是即可以整除400也可以整除100的为闰年。

#!/usr/bin/pythonyear = int(raw_input('Enter a year to determine leap year: '))if year % 100 == 0:        if year % 400 == 0:                print '%s is leap year' % year        else:                print "%s not is leap year" % yearelif year % 4 == 0:        print '%s is leap year' % yearelse:        print '%s not is leap year' % year

未完待续。。。

0 0
原创粉丝点击