Issue with Break

This is the place for queries that don't fit in any of the other categories.

Issue with Break

Postby Wommbatt » Thu Nov 21, 2013 8:37 am

So what I'm trying to do is make a store for my game. This store will have 4 menus in it when it's complete.
1. Main welcome to the shop menu to select which type of shopping you'd like to do
2. Attack skills menu
3. Other skills menu
4. Upgrade menu

Once the player has completed the shopping they wished to do I use a Break command to back out into the previous menu.
Before I started adding submenus the process worked out well enough. Now, however, the break commands generate errors. Specifically: SyntaxError: 'break' outside loop

Here is the relevant coding, although I suspect the issue is a misunderstanding on my part and not the code itself:

Code: Select all

# The main menu of the shop
def shop(exp):
    shopping = 'yes'
    print("Welcome to the Shop.")
    shopping = input("1 for attacks, 2 for skills> ")
    while shopping == '1':
        ATTACKS_menu(exp)
    while shopping == '2':
        SKILLZ_menu(exp)

# The Attacks purchasing menu
def ATTACKS_menu(exp):
            num = 1
            for i in ATTACKZ:
                # format: "1    ) Name, Cost"
                print(num, ")", str([i[0]])[2:-2], [i[4]], "exp\n")
                num += 1
            print("What would you like to buy?")
            print("When you've finished shopping type '0'.")
            choice = int(input("> ")) - 1  # -1 to compensate for numbering
            if choice == -1:
                break                 # this is where the error seems to come from
            else:
                purchase = choice
                buying(ATTACKZ[purchase][4], exp, purchase)


I've left out "buying()" and "ATTACKZ" because I don't believe they're related to the issue.
Oh, and of course "shop()" is referenced further on in the coding.

I believe that is everything. Questions, explanations, and suggestions are all welcome.
Thanks in advance,
-Wommbatt
Wommbatt
 
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm

Re: Issue with Break

Postby Kebap » Thu Nov 21, 2013 10:25 am

break can't be used to end functions early, it only works with loops, etc. What you probably search is called return.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 394
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Issue with Break

Postby Wommbatt » Thu Nov 21, 2013 11:39 am

BEAUTIFUL!
Thankyou so much for putting me on the right path sir. I had a feeling it was a misunderstanding on my part, and sure enough it was. I didn't realize you could use a return without passing any data and just go up a nesting level.

Learning is wonderful 8],
-Wommbatt
Wommbatt
 
Posts: 24
Joined: Thu Jun 13, 2013 2:15 pm

Re: Issue with Break

Postby Kebap » Thu Nov 21, 2013 12:04 pm

You're welcome. Glad it helps! :mrgreen:

Wommbatt wrote:I didn't realize you could use a return without passing any data and just go up a nesting level.


Technically, this will return a None value, but it won't matter here, as you don't even save the returned data anyway.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 394
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests