Something to help those working on the Scientific Computing with Python Budget App

I wrote a function to compare my assembled string for create_spend_chart vs. correct answer in the test module. I hope this helps the person who goes down this rabbit hole of trying to compare the formatted text and find what is wrong!

To use this code, pass your string to chart_check() and you will see output similar to this:

You need to remove the very last newline character!
my length: 404 vs. correct length 403
Mine...:Percentage spent by category...
Correct:Percentage spent by category...
Mine...:100|          ...
Correct:100|          ...
Mine...: 90|          ...
Correct: 90|          ...
Mine...: 80|          ...
Correct: 80|          ...
Mine...: 70|    o     ...
Correct: 70|    o     ...

And so on.

Here is the code:


    return return_str[:-1] 

# extra code to find mis-matched strings
def chart_check( my_answer):
    # copy the 'correct' string from

    if my_answer[-1] == '\n':
        print("You need to remove the very last newline character!")

    correct_answer = "Percentage spent by category\n100|          \n 90|          \n 80|          \n 70|    o     \n 60|    o     \n 50|    o     \n 40|    o     \n 30|    o     \n 20|    o  o  \n 10|    o  o  \n  0| o  o  o  \n    ----------\n     B  F  E  \n     u  o  n  \n     s  o  t  \n     i  d  e  \n     n     r  \n     e     t  \n     s     a  \n     s     i  \n           n  \n           m  \n           e  \n           n  \n           t  "
    correct_string_split = []
    my_string_split = []

    # Are they the same length
    if len(my_answer) != len(correct_answer):
        print(f"my length: {len(my_answer)} vs. correct length {len(correct_answer)}")

    # Lets look at each line in detail
    correct_string_split = correct_answer.split('\n')
    my_string_split = correct_answer.split('\n')

    # check each line to see if they match
    if len(correct_string_split) == len(my_string_split):
        for line in range(len(my_string_split) ):
            print('Mine...:' + my_string_split[line] + '...')
            print('Correct:' + correct_string_split[line] + '...')

            if my_string_split[line]  == correct_string_split[line] :
                #print ("->matching")
                print ("ERROR")
                print(f"my     length:{len( my_string_split[line] ) } ")
                print(f"correct length:{len(correct_string_split[line] )}" )

    longest_len = len(correct_answer)
    if len(my_answer) > longest_len:
        longest_len = len(my_answer) 
    for point_in_string in range(len(correct_answer)):
        if correct_answer[point_in_string] == my_answer[point_in_string]:
            print(f"At space number {point_in_string} ")
            print(f" mine: '{my_answer[point_in_string]}' vs correct: '{correct_answer[point_in_string]}'")

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.