Python自学记录 - 005

课程:
Microsoft: DEV236x
Introduction to Python: Absolute Beginner
课时:
Final Required Code
习题:
Program: adding_report() function
This program calls the adding_report() function which repeatedly takes positive integer input until the user quits and then sums the integers and prints a “report”.
The adding_report() function has 1 string parameter which indicates the type of report:

“A” used as the argument to adding_report() results in printing of all of the input integers and the total
“T” used as the argument results in printing only the total
Sample input and output:
call adding_report() with “A” as argument (print all the integers entered and the total)

Input an integer to add to the total or “Q” to quit
Enter an integer or “Q”: 3
Enter an integer or “Q”: 6
Enter an integer or “Q”: 24
Enter an integer or “Q”: 17
Enter an integer or “Q”: 61
Enter an integer or “Q”: nine
nine is invalid input
Enter an integer or “Q”: q

Items
3
6
24
17
61

Total
111
call with “T”(print only the total)

Input an integer to add to the total or “Q” to quit
Enter an integer or “Q”: 5
Enter an integer or “Q”: 7
Enter an integer or “Q”: Quit

Total
12

这道题!这道题!都快给我整疯了!!!
我严重怀疑,微软的讲师在设计题目讲解的时候故意弄了一些障碍。(传送门:link)
本来我自己拆解题目的时候,思路还是很清晰的,但是,反而看了课程关于这道题的一些思路,以及他们画出来的逻辑图,整个人都给整懵逼了= =
特别是他们的那个loop diagram,完全扰乱了我的思路,反反复复,这道题我起码写了3个还是4个版本的不同答案。
最终,我决定,干脆不管课程对于这道题的描述,完全按照自己的思路去走,反而比较轻松的在不到1小时里面做出来了…
我…大概我还是不太理解大佬们的想法和逻辑吧…

最终代码如下

report_type = input("Choose the Report Type(\"A\" or \"T\"): ")


def adding_report():
    total = 0
    item = ""
    report_one = input("Input an integer to add to the total or \"Q\" to quit: ")
    while report_one.startswith("q") != True:
        report = input("Enter an integer or \"Q\": ")
        if report.isdigit() == True:
            item = item + "\n" + report
            total = total + int(report_one) + int(report)
        elif report.lower().startswith("q") == True:
            if report_type.lower().startswith("a") == True:
                print("\nItem\n" + report_one + item + "\nTotal\n" + str(total))
                break
            elif report_type.lower().startswith("t") == True:
                print("\nTotal\n", total)
                break
            else:
                pass
        else:
            print(report, "is valid input.")
    else:
        pass


adding_report()

我自己倒是觉得逻辑很清楚,感觉自己棒棒哒!卡了快一周的期末作业,最终完全靠自己解出来了,当时那一瞬间的成就感,简直爆棚!转圈圈~~~~

这次期末作业给我最大的体会就是,编程的时候,思路一定要清楚,逻辑自己一定要理顺,不然写再多都是乱的,都是无用功。

加油啊宝贝儿,你真棒! Please insist and go on!

你可能感兴趣的:(Pyhon学习笔记)