简体   繁体   English

有没有办法安排output中的项目

[英]Is there a way to arrange items in the output

I'm trying to build an arithmetic formatter that takes in math problems, arranges them side by side and print them out.我正在尝试构建一个算术格式化程序来处理数学问题,将它们并排排列并打印出来。 My problem is that in the output, the operand of all the problems is changed to match that of the last problem and i don't know how to arrange them them side by side.我的问题是在 output 中,所有问题的操作数都更改为与最后一个问题的操作数相匹配,我不知道如何将它们并排排列。 How do i stop the operand from being changed in the output and how do i arrange the sums side by side.如何阻止操作数在 output 中被更改以及如何并排排列总和。 Thanks in advance.提前致谢。

def arithmetic_arranger(*problems):
    temp = "item1 + item2"
    print(f"Please enter your problems in this manner: {temp}")
    lines = "-------"
    # check number of problem first

    if len(problems) > 5:
        print("Error.Too many problems")
    else:
        pass

    # check the length of each number
    for i in problems:
        a = i.split(" ")
        val1 = a[0]
        val2 = a[2]
        if (len(val1)) > 4 or (len(val2)) > 4:
            print("Error: Numbers cannot be more than four digits.")
        else:
            pass

    # check the operator
    for i in problems:
        a = i.split(" ")
        operand = a[1]
        if operand == "+" and operand == "-":
            pass
        elif operand != "+" and operand != "-":
            print("Error.operand must be + or -")

    # test only digits
    for i in problems:
        a = i.split(" ")
        val1 = a[0]
        val2 = a[2]
        if val1.isnumeric() and val2.isnumeric():
            pass
        else:
            print("Error. Numbers must contain only digits")
    # carry out the problems and format it
    if operand == "-":
        result = float(val1) + float(val2)
    else:
        operand == "+"
        result = float(val1) - float(val2)
# print it out
    for i in problems:
        solution = "  {}\n{} {}\n{}\n\n".format(val1, operand, val2, lines)
        print(solution)

arithmetic_arranger("223 - 3144", "999 + 1313", "45 + 43", "123 + 49") arithmetic_arranger("223 - 3144", "999 + 1313", "45 + 43", "123 + 49")

This is the expected output:这是预期的 output:

   223       999      45      123
- 3144    + 1313    + 43    +  49
------    ------    ----    -----

You have a couple of logic errors.你有几个逻辑错误。 Ex: if operand == "+" and operand == "-": It can't be both例如: if operand == "+" and operand == "-":不能同时存在

Also, in the # test only digits loop , val1 and val2 get re-assigned in each iteration of the loop.此外,在 #test # test only digits loop中, val1val2在循环的每次迭代中都被重新分配。 So after the loop is completed, they have the values of the last items.因此在循环完成后,它们具有最后一项的值。 You can either make them lists, or move to printing code to inside that for loop.您可以将它们列成列表,也可以将代码打印到 for 循环内部。

I have combined everything into one loop.我已将所有内容组合成一个循环。

def arithmetic_arranger(*problems):
    # check the operator
    for i in problems:
        a = i.split(' ')
        operand = a[1]
        if operand != "+" and operand != "-":
            print("Error.operand must be + or -")
            continue
        if not (a[0].isnumeric() and a[2].isnumeric()):
            print("Error. Numbers must contain only digits")
            continue
        # carry out the problems and format it
        val1 = int(a[0])
        val2 = int(a[2])
        if operand == "-":
            result = int(val1) - int(val2)
        else:
            result = int(val1) + int(val2)
        # print it out
        solution = "  {0:5}\n{1:>2} {2}\n  {3:5}\n\n".format(val1, operand, val2, result)
        print(solution)

arithmetic_arranger("223 - 3144", "999 + 1313")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM