算术格式化程序项目-不是在水平线上而是在新线上给我解决方案

问题描述 投票:0回答:1

在此处输入图像描述```python def算术排列器(问题,show_answers = False): #错误处理:大于5的问题

    if len(problems) > 5:
        return 'Error: Too many problems.'

#splitting problems between operator and numbers
    split_problems = ''

    for problem in problems:           
        numberA, operator, numberB= problem.split()


#error handling of problems- only digits allowed

       if not (numberA.isdigit() and numberB.isdigit()):
         return 'Error: Numbers must only contain digits.'

#error handling of problems-no more than 4 digits 

       if len(numberA) > 4 or len(numberB) > 4:
            return 'Error: Numbers cannot be more than four digits.'

# Error handling of problems- incorrect operator 
       if operator not in ('+', '-'):
            return "Error: Operator must be '+' or '-'."

#define needed width

        width = max(len(numberA), len(numberB)) + 2

#arranging the sums

         split_problems += numberA.rjust(width) + '\n'
         split_problems += operator + numberB.rjust(width - 1) + '\n'
         split_problems += '-' * width + '\n'

     return split_problems

打印(f' {arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

For the code above, I'm not too sure how to show the solution in a horizontal line since my solution at the moment is one answer below the other instead of next to each other.
 

You don't need to show me the solution just please explain what I can do to have it in a horizontal line next to each other than below each other.

I'm getting:

xxx
+xx
___
xxx
-xx
___
xxx
+xx
___
xxx
+xx
___

而不是:

xxx   (four spaces) another here  (four spaces) another here (four spaces) another here 
+xx
___

所有缩进都是准确的 - 我只是不能在这里这样做 预先感谢!

python arithmetic-expressions
1个回答
0
投票

为此,您需要获取所有第一个数字并将它们放入列表中。然后,使用 for 循环并执行如下操作:

printstr = ""
for i in numbers:
    printstr = printstr + f"{i}\t"

然后,您可以打印 printstr 并对其余数字重复该过程。 (此外,您可以添加一个系统,对分隔方程和答案的线执行相同的操作)

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.