python:第三章 程序流程控制作业

题目

第三章 课后练习
1. 编写Python程序,将输入的百分制成绩转换为相应的等级,规则是:90分以上为’A’;8089分为’B’;7079分为’C’;6069分为’D’;60分以下为’E’;如果分数大于100或者小于0,则输出“成绩有误!”。
2. 编写Python程序,输出公元2000年~3000年之间所有闰年,每行输出4个年份
3. 编写程序,当输入一个正整数n时,求出n以内能被17整除的最大整数。
4. 编写程序,按下列要求完成操作:
	计算S=1+1/3+1/5+1/7+……+1/n,n由键盘输入。
5. 编写程序,在[11,100]范围内产生三个随机整数a、b、c,a求出a、b、c的最大公约数。
6. 修改第2题,增加功能为:输入提示“请输入成绩(按回车键退出):”;当输入的分数为大于100或者小于0时,则输出“成绩有误,请重新输入!”,并让用户重新输入,直至按回车键,程序结束。
*参考题:
编写程序,判断用户输入的8位字符密码强度,规则是:如果输入的字符长度不足8位或者超过8位的话,输出“密码长度不符合要求!!”;输入的字符只包含一种类型时,输出“密码强度为—低,建议修改”;输入的字符包含有二种类型时,输出“密码强度为—中,建议加强”;输入的字符包含有三种类型时,输出“密码强度为—强!”;输入的字符包含有四种类型时,输出“密码强度为—超强!!!”。
字符类型如下:数字09,小写字母a~z,大写字母A~Z,特殊字符(~@#$%^&*)。

作业程序

# 作者:王杰安 暨南大学 转载请注明出处
def report_code(oper):
    result = eval(oper)
    print('{} = {}'.format(oper, result))
def report_variable(str):
    print('{} = {}'.format(str, eval(str)))
def separate():
    print('##=============================##')
# 1
def score_rating(score):
    if score >= 90 and score <= 100:
        rating = 'A'
    elif score >= 80 and score <= 89:
        rating = 'B'
    elif score >= 70 and score <= 79:
        rating = 'C'
    elif score >= 60 and score <= 69:
        rating = 'D'
    elif score < 60 and score >=0:
        rating = 'E'
    else:
        rating = '成绩有误'
    print('Your score is [{}]. Your rating is {}'.format(score, rating))
    return rating
score_rating(93); score_rating(23); score_rating(64)
score_rating(73); score_rating(88); score_rating(110); score_rating(-2)
separate()
# 2
def leap_yeat_count(year_begin, year_end):
    n = 1
    for year in range(year_begin,year_end+1):
        if year % 4 == 0 and year % 100 != 0:
            if n <=3:
                n += 1
                print(year, end=' ')
            else:
                n = 1
                print(year)
        if year % 400 == 0:
            if n <=3:
                n += 1
                print(year, end=' ')
            else:
                n = 1
                print(year)
    print()
leap_yeat_count(2000, 3000)
separate()
# 3
def miuns_tool(n, m):
    if m>n:
        print('{} must samller than {}'.format(m, n))
    for i in range (n-m+1):
        if (n - i)%m == 0:
            print(n-i)
            break
miuns_tool(150, 17)
separate()
# 4
def cal(n):
    s = 0
    for i in range (int((n+1)/2)):
        s = s + 1/(1+2*i)
    print(s)
cal(n = 5)# n must be odd which is greater than 1
separate()
# 5
import random
def ran_cal(num_1, num_2):
    a = random.randrange(num_1, num_2)
    b = random.randrange(num_1, num_2)
    c = random.randrange(num_1, num_2)
    print(a,b,c)
    d = min([a, b, c])
    for i in range (d):
        if a%(d-i) == 0 and b%(d-i)==0 and c%(d-i)==0:
            print(d-i)
            break
ran_cal(500,1000)
separate()
# 6
while True:
    try:
        score = input('请输入成绩(按回车键退出):')
        if score == '':
            print('程序已退出。1')
            break
        if score_rating(float(score)) == '成绩有误':
            print('成绩有误,请重新输入')
    except:
        print('请输入正确的玩意!')
        continue
separate()
# 7
def define_password(password):
    num_type = [chr(48+i) for i in range(10)]
    small = [chr(97+i) for i in range(26)]
    capit = [chr(65+i) for i in range(26)]
    speacial = ['~','@','#','$','%','^','&','*']
    word_type = dict(zip(num_type+small+capit+speacial,['num']*len(num_type)+['small']*len(small)+['capit']*len(capit)+['special']*len(speacial)))
    password_type = dict(zip([1,2,3,4],['密码强度——低,建议修改','密码强度——中,建议加强','密码强度——强','密码强度——超强!']))
    if len(password) != 8:
        print('密码长度不符合要求!')
    type_count = len(set([word_type[word] for word in password]))
    print('Your Password is {}. '.format(password)+password_type[type_count])
define_password(password = '123qwe@#')
define_password(password = '@#DSDT1s' )

输出结果

Your score is [93]. Your rating is A
Your score is [23]. Your rating is E
Your score is [64]. Your rating is D
Your score is [73]. Your rating is C
Your score is [88]. Your rating is B
Your score is [110]. Your rating is 成绩有误
Your score is [-2]. Your rating is 成绩有误
##=============================##
2000 2004 2008 2012
2016 2020 2024 2028
2032 2036 2040 2044
2048 2052 2056 2060
2064 2068 2072 2076
2080 2084 2088 2092
2096 2104 2108 2112
2116 2120 2124 2128
2132 2136 2140 2144
2148 2152 2156 2160
2164 2168 2172 2176
2180 2184 2188 2192
2196 2204 2208 2212
2216 2220 2224 2228
2232 2236 2240 2244
2248 2252 2256 2260
2264 2268 2272 2276
2280 2284 2288 2292
2296 2304 2308 2312
2316 2320 2324 2328
2332 2336 2340 2344
2348 2352 2356 2360
2364 2368 2372 2376
2380 2384 2388 2392
2396 2400 2404 2408
2412 2416 2420 2424
2428 2432 2436 2440
2444 2448 2452 2456
2460 2464 2468 2472
2476 2480 2484 2488
2492 2496 2504 2508
2512 2516 2520 2524
2528 2532 2536 2540
2544 2548 2552 2556
2560 2564 2568 2572
2576 2580 2584 2588
2592 2596 2604 2608
2612 2616 2620 2624
2628 2632 2636 2640
2644 2648 2652 2656
2660 2664 2668 2672
2676 2680 2684 2688
2692 2696 2704 2708
2712 2716 2720 2724
2728 2732 2736 2740
2744 2748 2752 2756
2760 2764 2768 2772
2776 2780 2784 2788
2792 2796 2800 2804
2808 2812 2816 2820
2824 2828 2832 2836
2840 2844 2848 2852
2856 2860 2864 2868
2872 2876 2880 2884
2888 2892 2896 2904
2908 2912 2916 2920
2924 2928 2932 2936
2940 2944 2948 2952
2956 2960 2964 2968
2972 2976 2980 2984
2988 2992 2996 
##=============================##
136
##=============================##
1.5333333333333332
##=============================##
930 574 551
1
##=============================##
请输入成绩(按回车键退出):101
Your score is [101.0]. Your rating is 成绩有误
成绩有误,请重新输入
请输入成绩(按回车键退出):-11
Your score is [-11.0]. Your rating is 成绩有误
成绩有误,请重新输入
请输入成绩(按回车键退出):87
Your score is [87.0]. Your rating is B
请输入成绩(按回车键退出):99
Your score is [99.0]. Your rating is A
请输入成绩(按回车键退出):60
Your score is [60.0]. Your rating is D
请输入成绩(按回车键退出):34
Your score is [34.0]. Your rating is E
请输入成绩(按回车键退出):
程序已退出。
##=============================##
Your Password is 123qwe@#. 密码强度——强
Your Password is @#DSDT1s. 密码强度——超强!

好物分享
python: 数据科学代码速查表(强烈推荐!)
入门总结:
python入门:有关字符串的操作代码总结
python入门:有关math包以及内置函数的数值操作代码总结
Python练习:
python:第二章 字符串和数值程序作业
python:第三章 程序流程控制作业
python:第三章 程序流程控制作业2
python:第四章 列表与元组作业
python:第四章 列表与元组作业2

发布了20 篇原创文章 · 获赞 3 · 访问量 1496

猜你喜欢

转载自blog.csdn.net/qq_42830966/article/details/104946141
今日推荐