{"id":1691,"date":"2023-10-15T23:38:03","date_gmt":"2023-10-15T14:38:03","guid":{"rendered":"https:\/\/www.munchou.com\/biwa-chan\/?p=1691"},"modified":"2023-10-15T23:38:05","modified_gmt":"2023-10-15T14:38:05","slug":"python-make-a-huge-or-not-menu-not-in-a-while-loop","status":"publish","type":"post","link":"https:\/\/www.munchou.com\/biwa-chan\/?p=1691","title":{"rendered":"Python &#8211; Make a (huge or not) menu NOT in a while loop"},"content":{"rendered":"<p>The first big project I did during my studies was about the MVC architecture in which a main menu was needed. Having little knowledge in programming at that time, I created that menu IN a while loop, which is a mistake, because whatever called in that menu will be run in that loop, which can lead to crashes, if not the collapse of the Universe. I remember some weird stuff happened once or thrice, but I can&#8217;t recall what precisely. With practice, I came up (yes, by myself) with a menu that would NOT run in a while loop, but instead use external inputs that would use while loops and constrain the inputs&#8217; results to whatever the maker wants.<\/p>\n<p>In this short tutorial, I&#8217;ll show you how to create a menu, navigate to its elements and either &#8220;return&#8221; to the menu or quit the program.<\/p>\n<hr \/>\n<p style=\"padding-left: 40px;\"><strong><u>SETTING UP<\/u><\/strong><\/p>\n<p>Create your working folder and inside it 3 empty python files:<br \/><em>__init__.py<\/em><br \/><em>main.py<\/em> where our main program will be<br \/><em>menu_views.py<\/em> where we&#8217;ll put the inputs functions<br \/>Let&#8217;s start with <em>menu_views.py<\/em>.<\/p>\n\n<hr \/>\n<p style=\"padding-left: 40px;\"><strong><u>menu_views.py &#8211; CREATING THE INPUTS<\/u><\/strong><\/p>\n<p>That&#8217;s a very important part that covers one of the basics: the <strong>while loop<\/strong>. A while loop is a loop that&#8217;ll keep running as long as the conditions to get out of it aren&#8217;t met. Which can be &#8220;dangerous&#8221; as it can easily lead to an infinite loop.<br \/>For that program, I want 2 inputs:<br \/>&#8211; one to choose from the menu (think of it as a list from which you pick ONE item)<br \/>&#8211; one to choose to either go back to the menu or exit. That input is called in the item chosen from the menu (it&#8217;ll be clearer once you see the code)<\/p>\n<p>Create your first function: <span style=\"color: #003366;\"><strong>def choose_language():<br \/><\/strong><\/span>Directly, we start our loop with <span style=\"color: #003366;\"><strong>while True:<\/strong><\/span> and our whole input will belong to that loop. When the input is valid, it is returned and the program leaves the loop, else if runs again from the start.<br \/>I want to give the user 5 options: &#8220;1. English&#8221;, &#8220;2. French&#8221;, &#8220;3. German&#8221;, &#8220;4. Swedish&#8221;, &#8220;exit. Exit the program&#8221;. Here, you want to be careful as to where to place your input. If you place it BEFORE the options, the latter won&#8217;t show up, as the input &#8220;pauses&#8221; the program, waiting for the user to actually enter and validate something. So what I do is something like:<br \/>&#8220;Please choose a language:&#8221;<br \/>LIST HERE (&#8220;1. English&#8221; etc.)<br \/>INPUT: &#8220;Your choice:&#8221;<br \/>CHECK THE INPUT and process accordingly<\/p>\n<p>The tricky part, if I may write, with inputs is that if they aren&#8217;t properly checked, whatever entered by the user can be processed. For example, if you ask the user to enter &#8220;y&#8221; or &#8220;n&#8221;, the &#8220;y&#8221; will usually be checked and lead to something, whereas the &#8220;n&#8221; isn&#8217;t as the developer used a &#8220;else&#8221; or &#8220;elif&#8221; without enough constraints. I&#8217;ve seen it many times.<br \/>A super easy way to avoid that is to set the possible inputs in a list. So here it is, a way to do:<br \/>choices = [&#8220;1&#8221;, &#8220;2&#8221;, &#8220;3&#8221;, &#8220;4&#8221;, &#8220;exit&#8221;]<br \/>With that, the user does not have a choice but to enter &#8220;1&#8221;, &#8220;2&#8221;, &#8220;3&#8221;, &#8220;4&#8221; or &#8220;exit&#8221;. If not, then whatever the input was, the loop will just start over.<\/p>\n<p>So here is our little function:<\/p>\n\n\n\n<div class=\"codecolorer-container python railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">def<\/span> choose_language<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">while<\/span> <span class=\"kw2\">True<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; choices <span class=\"sy0\">=<\/span> <span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;1&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;2&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;3&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;4&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">&quot;exit&quot;<\/span><span class=\"br0\">&#93;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Please choose a language:&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;1. English&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;2. French&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;3. German&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;4. Swedish&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;exit. Exit the program&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; choice <span class=\"sy0\">=<\/span> <span class=\"kw2\">input<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;<span class=\"es0\">\\n<\/span>Your choice: &quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"kw1\">in<\/span> choices:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> choice<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;<span class=\"es0\">\\n<\/span><span class=\"es0\">\\t<\/span>Wrong input, please try again.<span class=\"es0\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">continue<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n\n\n<p>Cool! Now the function <strong><span style=\"color: #003366;\">def back_to_menu():<\/span><\/strong><br \/>Here, I simply want to ask if the user wants to go back to the menu or not. &#8220;y&#8221;=yes and &#8220;n&#8221;=exit the program. That function ONLY returns &#8220;y&#8221; or &#8220;n&#8221;, or loops again if the input is unexpected. That returned result will then be used in a different function to control the behavior of the program.<\/p>\n\n\n\n<div class=\"codecolorer-container python railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">def<\/span> back_to_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">while<\/span> <span class=\"kw2\">True<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; choice <span class=\"sy0\">=<\/span> <span class=\"kw2\">input<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Would you like to return to the main menu (y\/n)? &quot;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">casefold<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;y&quot;<\/span> <span class=\"kw1\">or<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;n&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> choice<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">else<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;<span class=\"es0\">\\n<\/span><span class=\"es0\">\\t<\/span>Wrong input, please try again.<span class=\"es0\">\\n<\/span>&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">continue<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n\n\n<p><strong><span style=\"color: #003366;\">.casefold()<\/span><\/strong> is a &#8220;trick&#8221; that converts whatever the user entered to lowercase. Therefore, even if the user mistakenly entered &#8220;Y&#8221; (instead of &#8220;y&#8221;), it would be changed into &#8220;y&#8221; and the program would be able to keep going instead of returning a &#8220;Wrong input&#8221; message, which could somehow upset the user.<\/p>\n\n<hr \/>\n<p style=\"padding-left: 40px;\"><strong><u>main.py &#8211; IMPORT THE INPUTS AND BUILD THE MENU<br \/><\/u><\/strong><\/p>\n<p>First thing to do: import the views by typing <strong><span style=\"color: #003366;\">import menu_views<\/span><\/strong> (you import your file as a python module). Once it&#8217;s done, we can call the functions we wrote in that file. It may sound like an obvious feature, but that&#8217;s something I find really cool, regardless of the language.<br \/><br \/>Create the function for your menu: <strong><span style=\"color: #003366;\">def main_menu():<\/span><\/strong><br \/>And directly call the target function by assigning it to a variable: <strong><span style=\"color: #003366;\">choice = menu_views.choose_language()<\/span><\/strong><br \/>Because the function we call is an input, it will directly be &#8220;started&#8221; and display to the user the above menu &#8220;Please choose a language:\u00a0 etc.&#8221;. And remember, it is a WHILE LOOP, so as long as the user hasn&#8217;t entered a valid input, the program won&#8217;t keep going on!<br \/>Which leads to what&#8217;s got to come right after: check what valid input the user entered. And that input is limited to only FIVE things: &#8220;1&#8221;, &#8220;2&#8221;, &#8220;3&#8221;, &#8220;4&#8221; or &#8220;exit&#8221;. Meaning you do NOT need to think about other possibilities and can focus on your actual menu. With that, we can simply use 5 <strong><span style=\"color: #003366;\">if<\/span><\/strong>, checking the value of <strong><span style=\"color: #003366;\">choice<\/span><\/strong>.<br \/>If choice = 1, then load the English language, and so on. If choice = exit, close the program.<br \/>That&#8217;s all! And you don&#8217;t need to use <strong><span style=\"color: #003366;\">elif<\/span> <\/strong>nor <strong><span style=\"color: #003366;\">else<\/span><\/strong>, because again: the choice is limited to a given list.<\/p>\n<p>I don&#8217;t know if there is an easier way to do, but I find that one extremely practical, logical, straightforward and easily maintainable. What? Who said I was boasting?!<\/p>\n\n\n\n<div class=\"codecolorer-container python railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">def<\/span> main_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; choice <span class=\"sy0\">=<\/span> menu_views.<span class=\"me1\">choose_language<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;1&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; English<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;2&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; French<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;3&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; German<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;4&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; Swedish<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;exit&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Byebyyyyyye&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exit<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n\n\n<p>What are those &#8220;English()&#8221;, &#8220;French()&#8221; and all, you ask? Wait a little more, let&#8217;s create our <strong><span style=\"color: #003366;\">def back_to_menu():<\/span><\/strong> function. As its name suggests, it will be used to go back to&#8230; no, not the future you fucking nerd, the menu, yes!<br \/>And it&#8217;s in that function that we&#8217;re going to call the one we wrote in the views. So again, create a variable and call it:<br \/><strong><span style=\"color: #003366;\">choice = menu_views.back_to_menu()<\/span><\/strong><br \/>That one returned only TWO things: &#8220;y&#8221; or &#8220;n&#8221;, so we can use <strong><span style=\"color: #003366;\">if<\/span> <\/strong>and <strong><span style=\"color: #003366;\">else<\/span><\/strong>, because each will be either one of the limited inputs. Maaan it&#8217;s really easy like that, isn&#8217;t it?<br \/>So we&#8217;ll do if choice = y, load the menu, else farewell amazing program.<\/p>\n\n\n\n<div class=\"codecolorer-container python railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">def<\/span> back_to_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; choice <span class=\"sy0\">=<\/span> menu_views.<span class=\"me1\">back_to_menu<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;y&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; main_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">else<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exit<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n\n\n<p>Now we just need to make those &#8220;English()&#8221;, &#8220;French()&#8221; etc. functions, in which we put our actions: just a print for that tutorial, followed by the function we just created above: <span style=\"color: #003366;\"><strong>back_to_menu()<\/strong><\/span>.<\/p>\n<p>Call the main function <strong><span style=\"color: #003366;\">main_menu()<\/span><\/strong> at the end of your file (or else nothing will happen when you run your program), and voil\u00e0\u00e0\u00e0!<\/p>\n\n\n\n<div class=\"codecolorer-container python railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/><\/div><\/td><td><div class=\"python codecolorer\"><span class=\"kw1\">import<\/span> menu_views<br \/>\n<br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> main_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; choice <span class=\"sy0\">=<\/span> menu_views.<span class=\"me1\">choose_language<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;1&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; English<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;2&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; French<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;3&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; German<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;4&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; Swedish<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;exit&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;Byebyyyyyye&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exit<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> back_to_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; choice <span class=\"sy0\">=<\/span> menu_views.<span class=\"me1\">back_to_menu<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> choice <span class=\"sy0\">==<\/span> <span class=\"st0\">&quot;y&quot;<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; main_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">else<\/span>:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; exit<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> English<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;You chose English&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; back_to_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> French<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;You chose French&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; back_to_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> German<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;You chose German&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; back_to_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"kw1\">def<\/span> Swedish<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">print<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;You chose Swedish&quot;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; back_to_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<br \/>\nmain_menu<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n\n\n<p>Run it, test it, you should be having something quite cool.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1701\" src=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2023\/10\/tuto_make_a_menu_result.png\" alt=\"\" width=\"387\" height=\"394\" srcset=\"https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2023\/10\/tuto_make_a_menu_result.png 387w, https:\/\/www.munchou.com\/biwa-chan\/wp-content\/uploads\/2023\/10\/tuto_make_a_menu_result-295x300.png 295w\" sizes=\"(max-width: 387px) 100vw, 387px\" \/><\/p>\n<p>You can use that method to create complex menus without having to worry about running a sub-menu in a loop from which you&#8217;ll never get out of.<\/p>\n<p><br \/>If I have made a mistake somewhere or forgotten anything, let me know!<\/p>","protected":false},"excerpt":{"rendered":"<p>The first big project I did during my studies was about the MVC architecture in which a main menu was needed. Having little knowledge in programming at that time, I created that menu IN a while loop, which is a mistake, because whatever called in that menu will be run in that loop, which can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1699,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[63,66,64],"_links":{"self":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1691"}],"collection":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1691"}],"version-history":[{"count":8,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1691\/revisions"}],"predecessor-version":[{"id":1702,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/posts\/1691\/revisions\/1702"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=\/wp\/v2\/media\/1699"}],"wp:attachment":[{"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.munchou.com\/biwa-chan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}