I am working on pset6 Mario. When I am running my program it is showing me some errors. But I think my code is correct.

Here is my code:

from cs50 import get_int

row = 0
space = 0
insert = 0
inputspace = 0

def main():
    while insert < 1 or insert > 8:
        insert = get_int("Enter a number: ")

def makepyramid(insert):
    while (row <= insert):
        while (insert < inputspace):
            inputspace = inputspace + 1


And this is the error I am getting:

Traceback (most recent call last):
  File "/home/ubuntu/pset/pset6/mario/mario.py", line 19, in <module>
  File "/home/ubuntu/pset/pset6/mario/mario.py", line 9, in main
    while insert < 1 or insert > 8:
UnboundLocalError: local variable 'insert' referenced before assignment

Please help me rectify my fault.

1 Answer 1


row does not exist/was not declared in your makepyramid function, so the IDE will throw an error when you try to use it there. You either need to pass row from your main function to makepyramid or make it a global variable.

EDIT: to better address the question and those brought up in a comment

For the most part, global variable is declared just like a normal one but outside/before your main function.

row = 0

def main():
   #the code you want in your main function
   #code that uses row

def makepyramid(insert):
   #code that you want in your function
   #code can also use row

Right now, you are also receiving the error for your main function where you call the makepyramid function because insert is a string. The input() function returns a string object, which is why you are needing to cast it to an int in your while loop declaration. You could, instead, cast the entire return of input() to an int, but then you run the risk of a non-integer input being given and crashing the program. You could also use CS50's get_int function in Python, to ensure that the value returned is an int.

To use the get_int function, you would need to import the correct library, but this is described in the documentation and the lessons. If you chose this route (hint), don't forget that you no longer need to cast insert to an int in your loop declaration.

Let me know if you still run into issues or need further assistance.

  • Thanks for your reply. But I don't quite understand how to make a global variable. Also, why is it showing an error in main(). Can you please help me with this. Jun 27, 2021 at 16:59
  • I edited my answer. Let me know if this still leaves you with questions. Jun 27, 2021 at 19:39
  • Thanks for your reply. It really help me. But now I am getting other errors. I have updated my question for you to see it. Jun 28, 2021 at 11:46
  • The last error (line 15 in makepyramid) is the same as you had before with your row variable (you just never got to this error before because the one for row crashed your program first). inputspace is not defined in makepyramid but in main, and so needs to be either passed to the new method along with insert or declared globally. This is what UnboundLocalError: local variable 'inputspace' referenced before assignment means. The rest of the readout is just the traceback to how the error was reached, and are not errors. Jun 28, 2021 at 11:53
  • The last error was solved. But It got replaced by other error. And also I still have not understood why the error is coming in main(). I have updated the code and the error that is coming. Please look into it. Jun 28, 2021 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .