While loop example


def loopyloop(T_or_F, arg, some_number):
print(“Method loopyloop is executing.”)

while T_or_F:
    print(f"This will not run if T_or_F is False.")
    print(f"If T_or_F is True, this will run continuously,until I make it False.")
    print(f" arguement = {arg} .")

    if some_number >= 0:
        print("My number is greater then 0. I can change the value,")
        print(" of the number in this if block. I can change any of my args.")
        print("changing the some_number to make the esle block execute.")
        some_number = -1
        print("changing the arg in the if block.")
        arg = "It's all wibbly wobley timey whimey stuff."
        print("I am not changing the T_or_F because I want it to continue running.")

    if some_number == -1:
        print("second 'if' statement.  You can have a million if's, and if every one of them is true, they will all execute.")
        some_number = 'Run you clever man.'

    elif some_number == 'Run you clever man.':
        print("Did this 'elif' execute after I changed the some_number in the if block?")
        # it will not execute if the preeceding if blocks ran.

    #print("This is changing arg outside of the (if-elif) block")
    #arg = "Weeping angels are terrifing."

        #else block only runs if all preceeding (if -  elif) blocks fail to run
        print("Else block is executing.")
        arg = "Would you like a jelly belly?"
    print("This is outside the if and else, but still inside the while.")
    print("While will check at the end to see if my T_or_F is 'True' or 'False'")
    print("If I do not make it false, it will run until the system errors out... hopefully.")
    print("Are the arguements still changed ?")
    print(f"arg = {arg}") 
    print (f"some_number = {some_number}")
    print("Ending the while loop:")
    T_or_F = False
    print("will this print?")

loopyloop(True, “Matt Smith”, 11)


Yeah, that’s basically the idea. Just a couple things here that are going to cause you trouble:

  • Words bool and int are actually reserved names in Python, so change them to something else.
  • You have inconsistent indents, but that might just be the paste you did here. Check the lines right after the second if int < 0 in your file.
  • You don’t need ( ) around your truth on if and while. Do:
  • You need the else for the if. See below.
while my_bool:

    if my_int > = 0:

What you should see is, if your first if covers all ints >= 0, then all that’s left are ints < 0. No need for a second if. In fact, that will trip you up every time, so remember that you always write the else: and then prove to yourself you don’t need it.

One thing to try is write the “shell” of python structures, then fill them in. So see how I have the code above? You write that, then the … gets filled in. That way you know the structure is right and you can work with it easier.