Ask coding questions

← Back to all posts
Why do I keep getting a recursion error?
AIDANGREEN6 (16)

I am trying to make a program in python that needs to use the random function. All the other parts of the program were already done and working. I made a new line at the beginning, and typed: import random
I ended up getting an error saying:
[RecursionError]
maximum recursion depth exceeded
exit status 1
I looked through the packages using the package option to the left of the screen instead, and couldn't find the random module. However, I found a module called random2. I tried to import it two ways ( writing import random2 in my code and going into the random2 module and clicking the plus button), but both times got the same error:
[RecursionError]
maximum recursion depth exceeded
exit status 1
Is there any way that I can fix this problem? Or is there some way to pick a random number (from 1 to 100) without importing a module?
(edit:)
I got rid of all of the random code, but am STILL getting the recursion error. So apparently the module wasn't the issue, but it didn't start until I tried to import the module.
edit: I'm kind of an idiot. I wasn't adding the repl because it was from repl teams under my friends account, but thanks to catspython, I realized that I could add a link to the repl.
https://replit.com/@KindaHours/Test?v=1

Comments
hotnewtop
CookieSnowOwl (41)

Probably because you have a function lets say x():
and in it you have the word
x():
#text
x()
and that is called a recursive function. To avoid losing your computer and getting mad, there is a preventing thing to stop it from "recursing" too much. you don't need a package. Just say
import random
x = random.randint(1, 100)
print(x)
that's done
or if ur doing
def y():
x = random.randint(1, 100)
print(x)
y()
Then that will have a recursion error.

AIDANGREEN6 (16)

@CookieSnowOwl so I can't use random.randint() in a different function? Sorry, I don't fully understand.

CookieSnowOwl (41)

No, it has nothing to do with random.randint.
A recursive function is when you call a function within the same function..
like

def y():
  y()

That will cause a recursion error.

CookieSnowOwl (41)

If there is, it's here.

def password_signin(name,password):
  if name == "m00sehead" and password == "46tg":
    signin("Aidan") 
  elif name == "KindaHours" and password == 'Yurpass' : 
    signin("Olukolade")
  elif name == "Guest" :
    signin("Guest")
  else:
    with open('Username.txt') as myfile:
     if " "+name+" "+password+" " in myfile.read():
      signin(name)
     else :
        f = open("Username.txt", "a")
        f.write(" "+name+" "+password+" ")
        f.close()
        signin(name)
  return        

because you are signin name with the same exact thing and if it is else it will always be else. That will recurse forever, and will raise a recursive error. don't do signin(name). do like input(what is your name) what is your password
then signin with the new one
in the if name + this thing in myfile.read
and signin(name)
name doesn't change and it will do that forever. And that will raise an recursive [email protected]

AIDANGREEN6 (16)

@CookieSnowOwl Ok, so I understand what your saying, but I don't understand what to replace, and what to replace it with. I also don't understand what you mean by name doesn't change. If you look at the bottom of all the code, it ask's what your name is and what your password is.

CookieSnowOwl (41)

so in here

def password_signin(name,password):
  if name == "m00sehead" and password == "46tg":
    signin("Aidan") 
  elif name == "KindaHours" and password == 'Yurpass' : 
    signin("Olukolade")
  elif name == "Guest" :
    signin("Guest")
  else:
    with open('Username.txt') as myfile:
     if " "+name+" "+password+" " in myfile.read():
      signin(name)
     else :
        f = open("Username.txt", "a")
        f.write(" "+name+" "+password+" ")
        f.close()
        signin(name)
  return        

what happens is you have a password and a name and if it's not admin name+password and it's a before-guest, you do signin(name). 1. that shouldn't work, signin has 2 parameters, you gave 1. Also, you didn't put namem = input("What is your username? ") in the function. so it will recurse and never ask you for the new ame. and you spelled it as namem. maybe that was on purpose. bye :)@AIDANGREEN6

AIDANGREEN6 (16)

@CookieSnowOwl
signin() has 1 parameter. password_signin() has 2. AS far as I can tell, you are thinking of password_signin() and signin() as the same functions. password_signin() acts like the actual sign in: checking if the username and password are correct. The signin() function is everything after that, which is the edit journal, read other people's journal, and watch animation options.
The namem was on purpose, because I wasn't sure how it would work if in the function password_signin(name,password) if I used the variable name as name.
However, for what you are saying about the recursion error, could a while True loop in a function cause that?

CookieSnowOwl (41)

Idk about the recursion error
yes while True is fine
but i found an error.

 if account == "Olukolade" :
    while True :
      whattodo = input("Would you like to: watch animation or add to notebook ?")
      if whattodo == "watch animation" :
        for x in range(0, 10):
          print("0____")
          time.sleep(0.1)
          print("_0___")
          time.sleep(0.1)
          print("__0__")
          time.sleep(0.1)
          print("___0_")
          time.sleep(0.1)
          print("____0")
          time.sleep(0.1)
          print("___0_")
          time.sleep(0.1)
          print("__0__")
          time.sleep(0.1)
          print("_0___")
          time.sleep(0.1)
      elif whattodo == "add to notebook" : 
        f = open("Olukolade.txt", "r") 
        print(f.read())
        f.close()
        add = input("What would you like to add to Olukolade.txt?")
        f = open("Olukolade.txt", "a")
        f.write(add)
        f.close()
      elif whattodo == "read another journal" :
        whattoread = input("Who's journal would you like to read?")
        f = open(whattoread+".txt", "r")
        print(f.read())
        f.close()
      else :
        print("Sorry, I did not understand what you said. Please say what you would like to do EXACTLY as shown in question. It is case sensitive.")
  if account == "Guest" :
    f = open("Guestbook.txt", "r")
    print(f.read())
    f.close()
    add = input("Please sign the guestbook. What is your name?")
    f = open("Guestbook.txt", "a")
    f.write(add)
    f.close()
  else:
    while True :
            whattodo = input("Would you like to: watch animation, add to notebook, or read another notebook ?")
            if whattodo == "watch animation" :
              for x in range(0, 10):
                print("0____")
                time.sleep(0.1)
                print("_0___")
                time.sleep(0.1)
                print("__0__")
                time.sleep(0.1)
                print("___0_")
                time.sleep(0.1)
                print("____0")
                time.sleep(0.1)
                print("___0_")
                time.sleep(0.1)
                print("__0__")
                time.sleep(0.1)
                print("_0___")
                time.sleep(0.1)
            elif whattodo == "add to notebook" :
              f = open(account+".txt", "a")
              add = input("What would you like to add to your file?")
              f.write(add)
              f.close()
              f = open(account+".txt", "r")
              print(f.read())
              f.close()
            elif whattodo == "read another journal" :
              whattoread = input("Who's journal would you like to read?")
              f = open(whattoread+".txt", "r")
              print(f.read())
              f.close()
            else :
              print("I do not understand what you want to do. Please type what you would like to do EXACTLY as shown in question. It is case sensitive.")
  return

is not in the function sign_in(account)@AIDANGREEN6

Coder100 (16875)

The random library is a native library that is included in python, you don't need to 'install' it.

AIDANGREEN6 (16)

@Coder100 Yes, but I still need to import it , correct? Because before I tried to import it, when I used the function random.randint(), it said it was an undefined function.

NathanTodd2 (7)

can you please include the repl

AIDANGREEN6 (16)

@NathanTodd2 I'm doing it on repl teams, and it's under my friends account.

NathanTodd2 (7)

@AIDANGREEN6 okay can you please ask him to repost

AIDANGREEN6 (16)

@catspython K, I'll add a link to the question

catspython (27)

@AIDANGREEN6 ok, I think you can't have that many layers in python, I mean you have 6 TABS! though I don't think you can fix it because python can't compute so many layers! I think the best thing to do is to rewrite the code so it doesn't have so many layers/tabs.

catspython (27)

@AIDANGREEN6 looking at the posts above I think that is wrong...

AIDANGREEN6 (16)

@catspython I just looked it up and it says the maximum is 5 layers. And I have 5. So, I guess that yeah, that could be the issue. I'll check the code and see if that did it, although I'm not sure how to do it with less layers. If I had the following (and yes, I know it's stupid, I just did it for an example)
'''
def bob() :
if bob == "bob"
print("bob")
if 1 = 1
if 2 = 2
if 3 = 3
if 4 = 4
bob()
'''
Would that count as 5 layers, or 7?

catspython (27)

@AIDANGREEN6 the whole idea, I just guessed what a recur. error is, they know.

catspython (27)

@AIDANGREEN6 some indents would help, I guess I was right....

catspython (27)

@AIDANGREEN6 do you mean

def bob():
 if bob == "bob":
  print("bob")
if 1 == 1:
 if 2 == 2:
   if 3 == 3:
    if 4 == 4:
     bob()

that's 5, or the longest indent chain, so the 2 def indents don't add up on the other 5 ones.
(use backticks "`" for code, 3 of them for code blocks)

AIDANGREEN6 (16)

@catspython Ok, so I'll have to take some of the lines and change them into separate functions, since they won't add up. Got it.

catspython (27)

@AIDANGREEN6 I hope that works! don't give me cycles until it works

catspython (27)

@AIDANGREEN6 Oh... you mean the def adding up because it's in the indents.... ok, I'm not sure if it adds up or not. That might be a prob....
edit:
ok I tested it, but it works, and I even added more layers with ifs into the bob() and if chain. IT WORKS! so you can ignore this.

NathanTodd2 (7)

@catspython each reply i get pinged you know