-1

I want this code to restart the question after someone doesn't put in the desired answer. I'm trying to use a while loop here, but something about it isn't working... Help! Also, this is a crude representation of a much much longer script.

def func_1():
  funce_again = True
  while funce_again == True:
    funce_again = False

  def mother():
    mothers_again = True
    while mothers_again == True:
      mothers_again = False

      print("1")
      print("2")
      print("\nPick a number.")
      dad = input("> ")
      if dad == "1":
        daddy = "1.0"
        bladder = "69"
      if dad == "2":
        daddy = "2.0"
        bladder = "70"
      else:
        mothers_again = True
        


      daddys_again = True
      while daddys_again == True:
        daddys_again = False
        os.system('clear')
        print(f"my mother is {bladder}")



  mother()


func_1()


  • 1
    Where do you call `func_1()`? Please create a [mcve] – OneCricketeer Sep 19 '22 at 19:17
  • Does this answer your question? [Asking the user until they give valid input](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – OneCricketeer Sep 19 '22 at 19:19
  • Probably unrelated: You can just delete out that `funce_again` variable and every line of code that contains it. Same with `daddys_again`. They do nothing. – JNevill Sep 19 '22 at 19:19
  • what is the point of the first 3 lines in the `func_1`? they could probably be removed or else safely commented out. Edit: same with `mother()` as well. – rv.kvetch Sep 19 '22 at 19:21

2 Answers2

0

Try adding a continue to your loop. Your logic is basically correct, but you need to restart the loop from the beginning once you detect an invalid input. continue moves the control of the program back to the top of the loop scope.

Edit: there is a second bug - you need to use an else if statement (elif in Python) instead of an else in your condition checking. Otherwise, when 1 is given as input the program still restarts the loop.

import os

def mother():
    mothers_again = True
    while mothers_again == True:
        mothers_again = False

        print("1")
        print("2")
        print("\nPick a number.")
        dad = input("> ")
        if dad == "1":
            bladder = "69"
        elif dad == "2":
            bladder = "70"
        else:
            mothers_again = True
            continue
        


        os.system('clear')
        print(f"my mother is {bladder}")



mother()
JRose
  • 1,382
  • 2
  • 5
  • 18
  • you forgot to address the fact that like 20% of the base code could probably be removed, without having any impact on what the program is doing. – rv.kvetch Sep 19 '22 at 19:27
  • @rv.kvetch OP never uses the useless function, so my guess is that they were testing a basic `while` loop with a condition they break on. I don't really see how this useless code is relevant to the question or why it needs to be addressed. They are asking a legitimate question with a clear problem and solution. I have removed the unimportant code from my answer. Edit: I noticed that OP messed up the formatting of their code so that `mother` is an inner function. I wonder if this was contributing the confusion about their question. – JRose Sep 19 '22 at 19:31
  • well, you still have some useless code in the example above, was only my point. e.g. are all variables above technically needed? example: the last `while` loop technically not needed (to be in loop) – rv.kvetch Sep 19 '22 at 19:35
  • @rv.kvetch The only unneeded variables I see are `daddy` (which OP may plan on using later but got stuck before it could be used?) and `daddys_again`. Here again it looks like OP may plan on having some sort of logic in this while loop that depends on `daddys_again` but while trying to debug their code they removed said logic. I cannot divine their true intention but OP really did have a bug in their code (lack of a `continue`) and their question reflects that they are aware that this is the bug. – JRose Sep 19 '22 at 19:38
  • agreed, as we cannot read the OP's mind, thus we cannot know the actual need of vars like `mothers_again` in code above. however, in these cases it is best to ask for clarification, rather than guess that they were attempting to debug code and thus removed the logic that used those vars. my meaning is, it's better to know for sure than to guess an intention. – rv.kvetch Sep 19 '22 at 19:41
  • Jordan, I fixed the second issue in your code relating to the input condition checking. @rv.kvetch I removed all useless logic to clarify the answer. – JRose Sep 19 '22 at 20:02
  • well, not exactly. `mothers_again` is technically not needed, but I guess this looks fine otherwise. – rv.kvetch Sep 19 '22 at 20:12
0

I think you've got it a little confused. This is how I would write your code:

def func_1():
    def mother():
        mothers_again = True
        while mothers_again:
            mothers_again = False
            print("1")
            print("2")
            print("\nPick a number.")
            dad = input("> ")
            if dad == "1":
                daddy = "1.0"
                bladder = "69"
            elif dad == "2":
                daddy = "2.0"
                bladder = "70"
            else:
                mothers_again = True

        daddys_again = True
        while daddys_again:
            daddys_again = False
            os.system('clear')
            print(f"my mother is {bladder}")

mother()

func_1()

There's no need to check

while mothers_again == True 

because

mothers_again 

¹is a boolean and will therefore always be equal to True Or False I've also changed your indentation so that

mother()

is actually called within

func_1()
James
  • 144
  • 6