4

How do i get my function to stop, when a condition has been met? for example in my following code, when the user inputs: "q" for (quit), i want my function to simply stop. i've tried using the "break" statement, but it's not working.

def main(): shape = input("Enter shape to draw (q to quit): ").lower() while shape != 'triangle' and shape != 'square' and shape != 'q': print("Unknown shape. Please try again") shape = input("Enter shape to draw (q to quit): ").lower() if shape == "q": print("Goodbye") break #Python not happy with the indentation. def get_valid_size(): size = int(input("Enter size: ")) while size < 1: print("Value must be at least 1") size = int(input("Enter size: ")) main() get_valid_size() 

when i run it, it executes:

Enter shape to draw (q to quit): q Goodbye Enter size:

i don't want it to ask for size.

2
  • what do you mean by stop? Commented May 10, 2015 at 5:23
  • @XiaotianPei i have a continuation for this code. when the user inputs "q", i dont want the function to continue doing the continuation. I dont know how to explain it in a programming way. Commented May 10, 2015 at 5:28

2 Answers 2

3

return will exit a function, returning control to whatever had originally called the function. If you want to learn more, the phrase to google is "Return statements."

break will exit a loop, as described here.

Try something like:

def main(): shape = input("Enter shape to draw (q to quit): ").lower() while shape != 'triangle' and shape != 'square' and shape != 'q': print("Unknown shape. Please try again") shape = input("Enter shape to draw (q to quit): ").lower() if shape == "q": print("Goodbye") return get_valid_size() def get_valid_size(): size = int(input("Enter size: ")) while size < 1: print("Value must be at least 1") size = int(input("Enter size: ")) main() 
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, the program is still not working as expected. it still continues to do the next step, when it shouldn't. i have edited my post to show the continuation of the program.
That's because of the way you have organized your code. You are calling main() and then (once main is complete, and control has RETURNED from it) you are calling get_valid_size(). I recommend calling get_valid_size from INSIDE main() after your check. Does that make sense? I updated the answer to reflect what I'm talking about. Eventually you would want to completely rewrite this code.
As an aside, you should spend some time reading about "Control Flow" (en.wikipedia.org/wiki/Control_flow) to better understand how to mentally walk through a program in the same way a computer would! It will make it much easier to solve this kind of problem yourself.
2

break is only used to exit for loops, while loops, and try loops.

return will exit a function with a specified value. Simply using return will return a None value, while using return True or return False will return true and false, respectively. You can also return a variable, for example, to return a variable x you would use return x.

1 Comment

Clarifying note: that technically wouldn't return the variable "x", but rather it would return the value stored in "x"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.