{"id":9251,"date":"2022-09-18T00:42:44","date_gmt":"2022-09-17T19:12:44","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/"},"modified":"2022-09-18T00:42:44","modified_gmt":"2022-09-17T19:12:44","slug":"solved-trying-to-solve-recursion-in-python","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/","title":{"rendered":"[Solved] Trying to solve recursion in Python"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-58539322\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"58539322\" data-parentid=\"58537143\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>tl;dr; The method defined is <strong>recursive<\/strong>, you can call it enough times to force an Exception of type <strong>Recursion Error<\/strong> because it adds to the stack every time it calls itself. Doesn&#8217;t mean it&#8217;s the best approach though.<\/p>\n<p>By <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Recursion_(computer_science)\">definition<\/a> a <strong>recursive<\/strong> function is one that is meant to solve a problem by a finite amount of statements while making that problem smaller each time, until you get to your base cases (which is when you return from that recursiveness). It also implies the function calling itself within the code. <\/p>\n<p>If we crudely put this in a bullet list style, a recursive function can be described by:<\/p>\n<ul>\n<li>Base cases, where returns happen in a function<\/li>\n<li>Function calls itself and passes the same data, but modified, &#8220;smaller&#8221; <\/li>\n<\/ul>\n<p>So far, the code you have pasted does <strong>one<\/strong> of these two. It calls itself within the function (although it doesn&#8217;t simplify the problem or pass data along). The function doesn&#8217;t have base cases to solve the problem with, it has but one case.<\/p>\n<p>The code pasted in the question seems to <strong>want<\/strong> to be an iterative forever loop but the <strong>way to achieve this<\/strong> has been done through recursive calls:<\/p>\n<p>When you execute the file that contains the provided code, the call outside of the definition will be executed <strong>once<\/strong>, but then inside the function, it calls itself again as long as the user doesn&#8217;t type <strong>quit<\/strong>. While it seems like it&#8217;s a fancy <strong>forever loop<\/strong>, this can be classified as a <strong>recursive<\/strong> method because it&#8217;s calling itself inside the function definition. Now, it&#8217;s a terrible recursive function, because you don&#8217;t need it to be recursive to work as intended. You never return anything, and every time you call the function <strong>str_replace_interface()<\/strong> you are adding to the Stack, which if you call it enough times you can cause a <strong>RecursionError<\/strong>. Let&#8217;s do just that to see it in action. To achieve, I will remove the code that lets the user quit, and also remove the input, so I don&#8217;t have to type a million times a word. <\/p>\n<pre><code>def str_replace_interface():   \n        str_replace_interface()\n\n\nstr_replace_interface() \n<\/code><\/pre>\n<p>When I executed, as expected I got the following:<\/p>\n<pre><code>Traceback (most recent call last):\n  File \"main.py\", line 8, in &lt;module&gt;\n    str_replace_interface()\n  File \"main.py\", line 5, in str_replace_interface\n    str_replace_interface()\n  File \"main.py\", line 5, in str_replace_interface\n    str_replace_interface()\n  File \"main.py\", line 5, in str_replace_interface\n    str_replace_interface()\n  [Previous line repeated 992 more times]\n  File \"main.py\", line 4, in str_replace_interface\nRecursionError: maximum recursion depth exceeded while calling a Python object\n<\/code><\/pre>\n<p>Why? Because the function never actually returns anything, that&#8217;s why I mentioned that making a <strong>forever loop<\/strong> recursively is terrible. You can achieve the same thing iteratively, as follows:<\/p>\n<pre><code>def str_replace_interface():   \n        word = input(\"Enter a word: \")\n        if word != 'quit':\n            substring = input(\"Please enter the substring you wish to find: \")\n            new_entry = input(\"Please enter a string to replace the given substring: \")\n            new_word = word.replace(substring, new_entry)\n            print(\"Your new string is: \" + new_word)\n            return True\n        return False    \n\nwhile str_replace_interface():\n  pass\n<\/code><\/pre>\n<p>Here, the power of the <strong>forever loop<\/strong> happens based on the <strong>returned value<\/strong> after executing the function. You are not adding to the stack with every call because the function actually returns. Now what happens if we force a continuous call over and over like we did for the recursive one? Well, the answer is nothing. You&#8217;ll have a forever loop&#8230;well, forever. But you won&#8217;t get a <strong>Recursive Error<\/strong> because the function executes and returns before calling it again. Give it a try:<\/p>\n<pre><code>def str_replace_interface():   \n        return True\n\nwhile str_replace_interface():\n  pass \n<\/code><\/pre>\n<p>See how it doesn&#8217;t raise an Exception? it just stays there hanging. You&#8217;ll have to forcefully kill the execution to make it stop, but an exception won&#8217;t happen. <\/p>\n<p>I hope that helps you clear it up. <\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Trying to solve recursion in Python <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] tl;dr; The method defined is recursive, you can call it enough times to force an Exception of type Recursion Error because it adds to the stack every time it calls itself. Doesn&#8217;t mean it&#8217;s the best approach though. By definition a recursive function is one that is meant to solve a problem by a &#8230; <a title=\"[Solved] Trying to solve recursion in Python\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/\" aria-label=\"More on [Solved] Trying to solve recursion in Python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[349,494],"class_list":["post-9251","post","type-post","status-publish","format-standard","hentry","category-solved","tag-python","tag-recursion"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Trying to solve recursion in Python - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Trying to solve recursion in Python - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] tl;dr; The method defined is recursive, you can call it enough times to force an Exception of type Recursion Error because it adds to the stack every time it calls itself. Doesn&#8217;t mean it&#8217;s the best approach though. By definition a recursive function is one that is meant to solve a problem by a ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-17T19:12:44+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Trying to solve recursion in Python\",\"datePublished\":\"2022-09-17T19:12:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/\"},\"wordCount\":538,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"python\",\"recursion\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/\",\"name\":\"[Solved] Trying to solve recursion in Python - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-17T19:12:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Trying to solve recursion in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Trying to solve recursion in Python - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Trying to solve recursion in Python - JassWeb","og_description":"[ad_1] tl;dr; The method defined is recursive, you can call it enough times to force an Exception of type Recursion Error because it adds to the stack every time it calls itself. Doesn&#8217;t mean it&#8217;s the best approach though. By definition a recursive function is one that is meant to solve a problem by a ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/","og_site_name":"JassWeb","article_published_time":"2022-09-17T19:12:44+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Trying to solve recursion in Python","datePublished":"2022-09-17T19:12:44+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/"},"wordCount":538,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["python","recursion"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/","url":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/","name":"[Solved] Trying to solve recursion in Python - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-17T19:12:44+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-trying-to-solve-recursion-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Trying to solve recursion in Python"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/9251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=9251"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/9251\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=9251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=9251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=9251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}