How to stop my function?

Moh'd H

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.

slifty

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()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to stop my javascript function?

From Dev

How to stop the beginning of my function's loop from repeating

From Dev

How to stop my service?

From Dev

How to stop recursive function?

From Dev

How to stop function on blur()

From Dev

How to stop interval function

From Dev

how to stop function in javascript

From Dev

How to stop this recursive function?

From Dev

How to stop recursive function?

From Dev

Python How to Stop my Loop

From Dev

How to stop execution in my program

From Dev

How to stop execution in my program

From Dev

Python How to Stop my Loop

From Dev

How to stop a video using stop function?

From Dev

Why won't my function stop?

From Dev

How can I stop having to type "$scope" before every variable and function in my Angular code?

From Dev

My "exit(0)" function prints ": Success" to stdout. How can I stop this?

From Dev

How do I stop my toggle function at the first and last list items?

From Dev

How to modify my readFile function so it does not stop at first line (java)

From Dev

How to stop my scroll function once MySql has fetched all the data

From Dev

How to stop Thread in other function

From Dev

How to stop a jquery draggable function?

From Dev

how stop javascript function pageScroll

From Dev

How to stop asynchronous function in JavaScript?

From Dev

How to stop function in event handler

From Dev

How to Stop a Recursive Function in JavaScript?

From Dev

Python: How to stop a looping function inside a function?

From Dev

How to stop a function in another function in corona sdk

From Dev

How do i stop my css animation