标题:求教大佬们啊啊啊啊,给你们坐下了
只看楼主
admin7588
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-12-26
 问题点数:0 回复次数:2 
求教大佬们啊啊啊啊,给你们坐下了
2018-12-26 17:17
FishK
Rank: 2
来 自:中国吉林延边
等 级:论坛游民
威 望:1
帖 子:4
专家分:10
注 册:2019-1-4
得分:0 
程序代码:
def get_input():
    print("请输入两个参数,第一个是整数,第二个是 add 或者 fact, 不区分大小写")
    while True:
        s = input(">")
        try:
            num, operate = s.split()
            num = int(num)
            return num, operate
        except ValueError:
            print("请按以下格式输入\n9 add\n4 fact")


def process(num, operate):
    if operate.lower() == 'add':
        output = add(num)
        print(output)
        add_record(output)
    elif operate.lower() == 'fact':
        output = fact(num)
        print(output)
        add_record(output)


def add_record(output):
    with open("jisuan.txt", 'a+') as f:
        f.write(output + "\n")


def add(num):
    sum = 0
    formula = ""
    n = []
    for i in range(2, num):
        if i % 2 == 0:
            sum += i
            n.append(str(i))
    formula = "+".join(n)

    return "输入 {} {}, {}={}".format(num, 'add', formula, sum)


def fact(num):
    sum = 1
    formula = ""
    n = []
    for i in range(1, num + 1):
        sum *= i
        n.append(str(i))
    formula = "*".join(n)

    return "输入 {} {}, {}={}".format(num, 'fact', formula, sum)


if __name__ == '__main__':
    num, operate = get_input()
    process(num, operate)


中国人不疑自己的多疑。
  -- 一九三六年,鲁迅《我要骗人》
2019-01-04 10:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-492303-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.571115 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved