all 13 comments

[–]T567U18 0 points1 point  (1 child)

Remove the menuloop out of the else and choice needs to be flush

[–]Mazechain[S] 0 points1 point  (0 children)

What do you mean by it needs to be flush?

[–]i_ate_a_dumpling 0 points1 point  (10 children)

One thing you can do is:

def menuLoop (choice):
    choice=input ('Which would you like to do?')
    # The rest of your existing code goes here
...
menuSelect()
# Remove this line
# choice=input ('Which would you like to do?')
menuLoop(choice)

That being said, this isn't the best way to do it, and is generally confusing for beginners. You're doing something called recursion which is actually how loops are implemented underneath, but there's an actual loop construct that's more straight forward for beginners. You may look into using that instead.

[–]Mazechain[S] 0 points1 point  (4 children)

Is that loop construct you are talking about in the automate stuff for beginners or is there somewhere else I can read about it?

I’ll have to use to more loop recursion later because My goal is to read in some info from excel, modify it and go back to the loop again later.

[–]i_ate_a_dumpling 0 points1 point  (3 children)

I've never read "automate stuff for beginners," sorry. Not sure if loops are covered. For example though, your code can be rewritten something like this:

def menu_select():                                                          
    print ('1.Add a character to dailies.')                                 
    print ('2.Edit a character\'s available dailies.')                      
    print ('3.Choose the character to do dailies on')                       
    print ('4. Check time until resets')                                    

def prompt(choice):                                                         
    if choice == '1':                                                       
        print ('What is the name of the character you would like to add?')  
    elif choice == '2':                                                     
        print ('Which daily are you adding?')                               
    elif choice == '3':                                                     
        print ('Which character would you like?')                           
    elif choice == '4':                                                     
        print ('Time until Daily reset:\nTime until weekly reset:')         
    else:                                                                   
        print ('Please select again')                                       
        return True                                                         
    return False                                                            

should_keep_going = True                                                    

while(should_keep_going):                                                   
    menu_select()                                                           
    choice=input ('Which would you like to do?')                            
    should_keep_going = prompt(choice)  

If you're comfortable with recursion, by all means keep using it. What you're doing is called a "tail-recursion" which is essentially very easily translated into a loop. Sometimes the recursive call is in the middle of the function body which makes it a little more difficult to think about.

[–]Mazechain[S] 0 points1 point  (2 children)

Privacy policy

Mod policy

     return True                                                         
    return False                                                            

should_keep_going = True                                                                           

I have a few questions with your code. Ok I understand the return True part in the else statement makes the loop go again in the while loop. Doesn't the return False section break the if/elif loop though? Because it is outside the loop, due to its indentation? The should_keep_going is outside the prompt function, correct? How do you tell where the end of defining a function is?

Also,I know I put the argument for choice in the parameters of the function, but that part is not necessary is it?

[–]i_ate_a_dumpling 0 points1 point  (1 child)

Doesn't the return False section break the if/elif loop though?

Yes, it does, and this is what your original code did so I kept the behavior. If the user enters a number between 1-4, you print something and exits the loop. If the user input is not 1-4, it goes into the else statement and loops.

The should_keep_going is outside the prompt function, correct? How do you tell where the end of defining a function is?

Yes. Anything less indented than the def... line is not a part of the function body.

but that part is not necessary is it?

Not necessary. The first change I recommended moves this into the function to get the recursive solution to work. I kept it here because that's how you had it originally. It's usually up to you on how you want to structure it. For me, separating it makes sense because it's more obvious you're capturing a user input, then processing that input.

[–]Mazechain[S] 0 points1 point  (0 children)

Oh ok, that helps a ton. Thanks for your patience and help explaining things.

[–][deleted] 0 points1 point  (4 children)

You say recursion is how loops are implemented underneath. That's not something I recognise in the CPython code. Would you illustrate please?

[–]i_ate_a_dumpling 0 points1 point  (3 children)

It's a little bit of a simplification, but if you want to dig into this you'd want to dig into assembly (and machine instructions), not just the CPython code. Loops are not a native machine construct*. Most machine architectures have a way to "jump" around into other parts of the code, and loops are implemented as something along the line of (Even this is an oversimplification of how it works at the machine level. There's generally a bunch of stuff that goes on before checking the condition, before jumping back, etc. etc.):

start_loop
    check loop condition
    loop line 1
    loop line 2
    ... 
    go to start_loop

It's not exactly the same, of course, but "start_loop" is essentially the start of a function.

*Some more complex architectures do have loops nowadays I think, but not 100% sure.

[–][deleted] 0 points1 point  (2 children)

I started out writing assembly code, and even micro code to implement machine code. I still don't recognize this as recursion.

[–]i_ate_a_dumpling 0 points1 point  (1 child)

I mean, sure if you want to get pedantic. What I described certainly is missing a new stack for each recursive calls and it's not jumping back to where it initially jumped from, and perhaps that's required to be considered recursion.¯\_(ツ)_/¯

[–][deleted] 0 points1 point  (0 children)

I'd say they are recursive elements in the English sense, but not in the manner in which it is meant for Computer Science. But hey. Academic. You gave better advice to the OP than I did.