Python Forum

Full Version: Why it won't run?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
https://replit.com/@hansemso/arithmeticf...er#main.py

Why won't it run? I'm doing the freecodecamp Python test #1. This is somebody's sample I found on youtube. I copied it word for word so I can study it. When she runs it it runs. But I can't make it run. Help! Thanks :) The replit address is above. I also copied and pasted it below. It is not numbered like in replit, but I checked the tabbing. I just click run like she does in the video. Here is a link to the video: https://www.youtube.com/watch?v=ZBJUihOVvVM&list=PPSV

Han Huh

def arithmetic_arranger(problems, *args):
     if len(problems) > 5:
         return "Error: Too many problems."


     arranged_problems = [ ]


     for index, value in enumerate(problems):
         # ["32", "+", "8"]
         operation = value.split(" ")


        if operation[1] not in "-+":
            return "Error: Operator must be '+' or      
            '-'."


        if len(operation[0]) > 4 or len(operation[2]                
        ) > 4:
            return "Error: Numbers cannot be more                       
            than four digits."


        try:
            value_1 = int(operation[0])
            value_2 = int(operation[2])
        except ValueError:
            return "Error: Numbers must only                            
            contain digits."


        
        #calculate the length of each line
        longest_val = max(len(operation[0]), len                    
        (operation[2]))     
        width = longest_val + 2


        # operation = ["32", "+", "8"]
        # output = f"{operation[0]:>{width}}\n{f'                  
       {operation[1]} {operation[2]}':>{width}}\n                 
       {'-'*width}"
                          
        L1 = f"{operation[0]:>{width}}"
        L2 = operation[1] + f"{operation[2]:>                       
        {width-1}}"
        d = '-' * width




        try:
            arranged_problems[0] += (' ' * 4) + L1
        except IndexError:
            arranged_problems.append(L1)


        try:
            arranged_problems[1]  +=  (' ' * 4) + L2
        except IndexError:
            arranged_problems.append(L2)


        try:
            arranged_problems[2] += {' ' * 4} + d          
        except IndexError:
            arranged_problems.append(d)


      
        if args:  
            """
            This runs if the second parameter True is                   
            passed in denoting we need to calculate                     
            the answer value.
            """
            ans = int(operation[0]) + int(operation[2]                  
            )  if operation[1] == '+' else int                          
            (operation[0]) - int(operation[2])
            a = f"{str(ans):>{width}}"


            try:
                arranged_problems[3] += {' ' * 4} + a
            except IndexError:
                arranged_problems.append(a)




    output = f"{arranged_problems[0]}\n                                               
    {arranged_problems[1]}\n{arranged_problems[2]}"
    output = output + f"\n{arranged_problems[3]}"               
    if args else output


    return output  




# print(arithmetic_arranger(["3 + 855", "3801 - 2",
#"45 + 43", "123 + 49"]))


# print("  3     3801     45     123\n+ 855
#-   2   +43    +  49\n----    -----    ----
#-----")


# print(arithmetic_arranger(["32 - 698", "1 - 3801", 
# "45 + 43", "123 + 49"], True)}


# print("    32    1    45    123\n-    698
# - 3801    + 43    +  49/n-----    ------    ----
#-----\n -666    -3000    88    172"}
Please post code and describe how you run the program.
I edited and updated my post with the code. Thanks :)
Your program is riddled with errors. Indentation is all over the place, and indentation is important in python. You break up lines without using a line continuation character. You have a big function, but you never call the function.
Oh, I see. That's very helpful. I did watch all the freecodecamp videos multiple times so have an idea. Thank you. What I don't get though is how did the lady in the youtube video run this program and it worked? Because I copied it word for word.
Maybe the problem is you did copy the code too exactly. For example, this is not valid python syntax:
        if operation[1] not in "-+":
            return "Error: Operator must be '+' or      
            '-'."
 
But this is:
        if operation[1] not in "-+":
            return "Error: Operator must be '+' or '-'."
This could be a problem if the video is showing code with word-wrap turned on (a bad, bad, bad idea).

Another code problem is your indenting all over the space. Configure your editor to use spaces for tabs, and each tab should be 4 spaces. It would be difficult for you to see an indenting error on a video.
Oh, I see. So it is that sort of problem, which I figured it probably would be. I even thought it might be a software version conflict issue. Anyway, I will just keep moving forward then until I have enough skill to not have to watch a video to pass a test. Thank you so much for your help. God bless :)

Han