How can I use conditional to control a while loop between a helper and main() function?

Question:

Directions:

  • Write a helper function to validate user input. Function should take inputs until valid. Invalid inputs return error message and repeat the prompt. Valid inputs return to main().

  • User inputs "1" to "5" each print a unique string. User input "0" exits program.

  • Everything should be executed from within a main() function. Only the call to main() and function definitions are global.

      import sys
      def getValidInput(selection):
          if selection == "1":
              return print("message_1")
          elif selection == "2":
              return print("message_2")
          elif selection == "3":
              return print("message_3")
          elif selection == "0":
              print("goodbye")
              sys.exit()
          else:
              return print("That is not a valid option.")
    
      def main():
          askAgain = True
          while askAgain == True:
              getValidInput(selection = input("Select number 1, 2, 3. 0 exits program."))
    
      in __name__ == "__main__":
          main()
    

My question/issue is that while the sys.exit works to terminate the execution, I have a feeling it’s not supposed to be what I’m using. I need to be able to terminate the program after user inputs "0" using a while loop. Within main(), I tried adding a false conditional to the function
call within main():

        askAgain = True
        while askAgain == True:
            getValidInput(selection = input("Select number 1, 2, 3. 0 exits program."))        
        if getValidInput(selection = input("Select number 1,2, 3. 0 exits program")) == "0"
            askAgain == False

and I tried adding the while loop conditional within getValidInput(), instead of main():

        askAgain = True
        while askAgain == True:
            if selection == "1":
                askAgain == True
                return print("message_1")
            if selection == "0"
                askAgain == False
                return print("goodbye")

What am I doing wrong here? I know I’m not using the loop conditional correctly, but I’ve tried it several different ways. The sys.exit() is my backup. The user should be able to enter inputs until 0 is entered and that should be controlled from within main(). How can I use values inputted in getValidInput to change the value of my while loop conditional in main()?

Asked By: Jos'gre

||

Answers:

You don’t need to have the while loop in your helper function. Your helper function is supposed to do one thing, which is just to check the answer and do something.

What you can do is instead of system.exit, each condition return a bool value and pass it to askAgain.
askAgain = getValidateInput()

Finally a small thing, to follow PEP8 rule, you should use snake case: so asg_again, get_validate_input instead.

Answered By: J_yang

If the program is required to run infinitely until the user inputs 0, your idea to use a boolean variable was already in a right path. However, you can utilize the getValidInput() function to return/assign whether the program should stop or continue running.

Assigning the variable selection in the function call inside the main() function is not needed.

One more tip is, you should be more careful on the brackets.

  import sys
  def getValidInput(selection):
      if selection == "1":
          print("message_1")
      elif selection == "2":
          print("message_2")
      elif selection == "3":
          print("message_3")
      elif selection == "0":
          print("goodbye")
      else:
          print("That is not a valid option.")
      
      # Use this if-else block to determine whether your function should continue running or stop
      if selection == '0':
          return False
      else:
          return True
 
  def main():
      askAgain = True
      while askAgain == True:
          askAgain = getValidInput(input("Select number 1, 2, 3. 0 exits program."))

  if __name__ == "__main__":
      main()
Answered By: Dhana D.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.