Python输出加减法竖式

2024-01-09 19:45:52
oper = input("输入运算符号(目前只支持+/-)")
if (oper == "+"):
    maxlen = [input(),input()]
    a, b = map(int, maxlen)
    r = a + b
    maxlen.append(str(r))
    maxlen = max(len(s) for s in maxlen)
    print(f'{a:>{maxlen+4}d}')
    print(f'{"+":>3s}{b:>{maxlen+1}d}')
    print(f'{"-"*(maxlen+4):>{maxlen+5}s}')
    print(f'{r:>{maxlen+4}d}')
elif oper == "-":
    maxlen = [input(), input()]
    a, b = map(int, maxlen)
    r = a - b
    maxlen.append(str(r))
    maxlen = max(len(s) for s in maxlen)
    print(f'{a:>{maxlen+4}d}')
    print(f'{"-":>3s}{b:>{maxlen+1}d}')
    print(f'{"-"*(maxlen+4):>{maxlen+5}s}')
    print(f'{r:>{maxlen+4}d}')

感谢【micthis】提供的帮助

文章来源:https://blog.csdn.net/joe_g12345/article/details/135421756
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。