Home python TracEBack error (Most Recent Call Last): (Python)

TracEBack error (Most Recent Call Last): (Python)

Author

Date

Category

wrote code that generates random passwords here is actually the code itself:

import random
From Random Import Choice
From String Import ASCII_UPPERCASE
DEF test ():
  CS = Random.Randint (8.25)
  Symbols = 'abcdefghijklnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890_'
  Rand = ('' .join (Choice (Symbols) for i in Range (CS))
  if Rand == "testpin12345":
    Print (Rand)
  ELSE:
    Test ()
Test ()

In the idea, it should work until it generates a random password that coincides with the value in if Rand == "testpin12345":
But the error crashes:

traceback (most recent call last):
 File "C: \ Test \ Test.py", Line 19, In ​​& Lt; Module & GT;
  Test ()
 File "C: \ Test \ Test.py", Line 17, In Test
  Test ()
 File "C: \ Test \ Test.py", Line 17, In Test
  Test ()
 File "C: \ Test \ Test.py", Line 17, In Test
  Test ()
 [PREVIOUS LINE REPEATED 1017 More Times]
 File "C: \ Test \ Test.py", Line 12, In Test
  Rand = ('' .join (Choice (Symbols) for i in Range (CS))
 File "C: \ Test \ Test.py", Line 12, In & Lt; GeneXPR & GT;
  Rand = ('' .join (Choice (Symbols) for i in Range (CS))
 File "C: \ Users \ Epicb \ AppData \ Local \ Programs \ Python \ Python38 \ Lib \ Random.py", Line 288, In Choice
  i = Self._randBelow (LEN (SEQ))
 File "C: \ Users \ Epicb \ AppData \ Local \ Programs \ Python \ Python38 \ lib \ random.py", Line 254, in _randbelow_with_getrandbits
  K = N.Bit_Length () # DON'T USE (N-1) HERE BECAUSE N CAN BE 1
Recursionerror: Maximum Recursion Depth Exceeded While Calling A Python Object

Answer 1, Authority 100%

You exceeded the recursion limit (calling the test function from yourself). You can see it like this:

import sys
Print (sys.getRecursionLimit ()) # 1000

This limit is needed not to overflow the stack, since Python does not optimize the tail recursion.

You should rewrite this code dynamically:

def test ():
  IS_First = True.
  While is_first or Rand! = "testpin12345":
    IS_First = False.
    CS = RANDINT (8, 25)
    Symbols = 'abcdefghijklnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890_'
    Rand = ('' .join (Choice (Symbols) for i in Range (CS))
  Print (Rand)

Enlarge recursion limit can be:

sys.setrecursionlimit (N)

But it is not recommended to do so. In addition, it is impossible to determine the exact number of calls in your code.


Answer 2

Your code is that you generate passwords until it is generated by “testpin12345”, and the function is not called in a cycle, but a call in the call, so the stack is quickly overwhelmed and everything, the end

Do not do such uncontrollable nested calls

Here is a working code:

import random
DEF test ():
  cs = random.randint (8, 25)
  Symbols = 'abcdefghijklnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890_'
  RAND = '' .JOIN (Random.choice (Symbols) for i in range (CS))
  Print (Rand)
Test ()

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions