I thought I should take a few days and read some more, and learn a little more. I decided to take a more easier approach. This 'start' of a simple pirate game, which I did not reference anything for once. So its all wrote from what I know, and not someone else. That said, you can critique it now. It is still very early function of what will become the Shipyard. So overhauling is a must.

# Pirates First function.
# Variables set.

gold = 300
price = 0



def Ship():
    print 'Welcome to the Shipyard!\n'
    print 'Here you can buy new Ships.\n'
    Shiplist = {'1)Sloop':'1500$', '2)Schooner':'3500$', '3)Brigantine':'6500$'}
    print 'The current Ships avaliable:\n'
    print Shiplist
    print ' '
    print 'To Exit type 4'
    print 'Type a number to choose the corrosponding Ship!\n'
    


    choice = input('Enter Number: ')

    if choice == 1:
        global gold
        price = 1500
        if gold >= price:
            print 'You bought a Sloop!\n'
            gold -= price
            print'You now have:',
            print gold,
            print 'Gold'
        elif gold < price:
            print 'You do not have enough Gold!'

    if choice == 2:
        global gold
        price = 3500
        if gold >= price:
            print 'You bought a Schooner!'
            gold -= price
            print 'You now have:',
            print gold,
            print 'Gold'
        elif gold < price:
            print 'You do not have enough Gold!'

    if choice == 3:
        global gold
        price = 6500
        if gold >= price:
            print 'You have bought a Brigantine!'
            gold -= price
            print 'You now have:',
            print gold,
            print 'Gold'
        elif gold < price:
            print 'You do not have enough Gold!'

    if choice == 4:
        print 'Exit'

You should add an else statement at the end of the checking, so that if I enter anything other than 1 to 4, it'll tell me I gave bad input and then it'll let me re-enter until it's correct.

Also, you can rewrite your if statements because they use a lot of the same code, so you can put it all into one place. Here's just an example:

# beginning of your code snipped here...

    choice = input('Enter Number: ')

    if choice in [1, 2, 3]:
        global gold
        
        if choice == 1:
            ship = 'Sloop'
            price = 1500
        elif choice == 2:
            ship = 'Schooner'
            price = 3500
        elif choice == 3:
            ship = 'Brigantine'
            price = 6500
        
        if gold >= price:
            print 'You bought a ' + ship + '!\n'
            gold -= price
            print'You now have:',
            print gold,
            print 'Gold'
        elif gold < price:
            print 'You do not have enough Gold!'
        
    elif choice == 4:
        print 'Exit'
        
    else:
        print 'Invalid input!'

If you wanted to keep the dictionary you use for listing the ship name and price, there's also an easier way for getting the ship name and price without using so many manual if statements, but may prove a bit complicated for you...

# beginning of your code snipped here...

    choice = input('Enter Number: ')

    if choice in [1, 2, 3]:
        global gold
        
        # get the names of the ships as a list
        shipnames = Shiplist.keys()

        # sort it (each name starts with "#)" so it'll be in the right order)
        shipnames.sort()

        # get the index the user entered (minus 1, because lists start at 0)
        ship = shipnames[choice - 1]

        # get the price without the "$", and turn it into an int
        price = int(Shiplist[ship][:-1])

        # remove the "#)" from the shipname
        ship = ship[2:]

        # etc...

Just remember, there are tons of ways of doing things...

Thats cool, I can change some of that around for sure. My biggest issue here is I need to understand how to exit things better and do loops more efficient that I know how as of now. basic structure of this I am thinking of is the Functions at the top, then as I complete them I can write most of the other program and have these functions that the player can call upon through options. And then somehow put them back into action after calling a function... That confuses me.

Ok, I changed up a few things, I also added a 'Rep' Feature now. And made the start up loop to the main menu. And you can access the Shipyard from the menu and it now loops back to the menu when done. Also added a line to show your money while in the shipyard.

# Pirates First function.
# Variables set.

gold = 300
price = 0
ship = 0
rep = 1

def S():
    print 'Welcome to the Shipyard!\n'
    print 'Here you can buy new Ships.\n'
    print 'You currently have',gold,'Gold.\n'
    
    Shiplist = {'1)Sloop':'1500$', '2)Schooner':'3500$', '3)Brigantine':'6500$'}
    print 'The current Ships avaliable:\n'
    print Shiplist
    print ' '
    print 'To Exit type 4'
    print 'Type a number to choose the corresponding Ship!\n'
    


    choice = input('Enter Number: ')

    if choice in [1, 2, 3]:
        global gold
        global price
        global ship
        exit = 4

        if choice == 1:
            ship = 'Sloop'
            price = 1500

        elif choice == 2:
            ship = 'Schooner'
            price = 3500

        elif choice == 3:
            ship = 'Brigantine'
            price = 6500

        

        else:
            print 'Invalid input!'
            

        if gold >= price:
            print 'You bought a new ship!'
            gold -= price
            print 'You now have:',
            print gold,
            print 'Gold\n'

        elif gold < price:
            print 'You do not have enough Gold!\n'
            print 'Try again later!\n'

        
        elif choice == 4:
            print 'Exit\n'

def Gold():
    global gold

def Rep():
    global rep
    rep = rep + 1
    print 'You have gained more reputation!\n'
    print 'Your Reputation is now:',
    print rep

choice = 0
loop = 1


while loop == 1:
    print 'Welcome to Pirates!\n'
    print 'Please Pick one of the following options!\n'
    
    print 'Type the options as shown!'
    print '1)Type: S() to enter the Shipyard!' 
    choice = input('Input an option! \n')

Okay, I have restructured some more things. I have also added a Tavern and a Black Smith shop. I still need to make some statements to check things, and re do the exit loop from the main menu, but its slowly coming along. The next huge thing will be adding different Missions to preform.

I am not sure how big is to big for python code, but its already huge, and most likely will double in size..

# Pirates First function.
# Variables set.

gold = 600
price = 0
ship = 0
rep = 1
weapon = 0

def S():
    print 'Welcome to the Shipyard!\n'
    print 'Here you can buy new Ships.\n'
    print 'You currently have',gold,'Gold.\n'
    
    
    Shiplist = {'1)Sloop':'1500$', '2)Schooner':'3500$', '3)Brigantine':'6500$'}
    print 'The current Ships avaliable:\n'
    print Shiplist
    print ' '
    print 'To Exit type 4'
    print 'Type a number to choose the corresponding Ship!\n'
    


    choice = input('Enter Number: ')

    if choice in [1, 2, 3]:
        global gold
        global price
        global ship
        exit = 4

        if choice == 1:
            ship = 'Sloop'
            price = 1500

        elif choice == 2:
            ship = 'Schooner'
            price = 3500

        elif choice == 3:
            ship = 'Brigantine'
            price = 6500

        

        else:
            print 'Invalid input!'
            

        if gold >= price:
            print 'You bought a new ship!'
            gold -= price
            print 'You now have:',
            print gold,
            print 'Gold\n'

        elif gold < price:
            print 'You do not have enough Gold!'
            print 'Try again later!\n'

        
        elif choice == 4:
            print 'Exit\n'

def Tav():
    print 'Hello There! Welcome to the Tavern!\n'
    print 'Here you can purchase some beer or liquor!\n'
    print '1)Beer 10$'
    print '2)Liquor 20$'
    print '3)Exit\n'
    print 'Enter 1, 2, 3\n'
    
    choice = input('What will it be? ')

    if choice in [1, 2]:
        global gold
        global price
        exit = 3

        if choice == 1:
            price = 10
            if gold >= price:
                gold -= price
                print 'You bought a beer!\n'
                print 'You now have',gold,'Gold\n'
            elif gold < price:
                print 'You do not have enough gold!'

        if choice == 2:
            price = 20
            if gold >= price:
                gold -= price
                print 'You bought a shot of liquor!\n'
                print 'You now have',gold,'Gold\n'
            elif gold < price:
                print 'You do not have enough gold!'
            
        
def Gold():
    global gold

def Rep():
    global rep
    global gold
    rep = rep + 1
        
    print 'You have gained more reputation!\n'
    print 'Your Reputation is now:',
    print rep

def Sm():
    print 'Hello! I am Gene Daniels, the Black Smith.'
    print 'What can I do for you today?'
    print 'Here is what I have right now.\n'
    wList = {'1)Short Sword': '100$', '2)Long Sword': '250$', '3)Old Cutlass': '400$'}
    print wList
    print 'Type: 1, 2, or 3:','Type 4 to exit\n'
    choice = input('What can I get you? ')

    if choice in [1, 2, 3]:
        global gold
        global price
        exit = 4

        if choice == 1:
            price = 100
            weapon = 'Short Sword'
            

        if choice == 2:
            price = 250
            weapon = 'Long Sword'

        if choice == 3:
            price = 400
            weapon = 'Old Cutlass'

        else:
            print 'Invalid Input!'

        if gold >= price:
            print 'You bought a new weapon!'
            gold -= price
            print 'You now have',gold,'Gold\n'

        elif gold < price:
            print 'You do not have enough Gold!\n'

        elif choice == 4:
            print 'Exit'
            
            
            
            
choice = 0
loop = 1


while loop == 1:
    print 'Welcome to Pirates!\n'
    print 'Please Pick one of the following options!\n'
    
    print 'Type the options as shown!\n'
    print '1)Type: S() to enter the Shipyard!'
    print '2)Type: Tav() to enter the Tavern!'
    print '3)Type: Sm() to enter the Black Smith Shop!'
    print '4)Type: 4 to exit\n'
    choice = input('Input an option! \n')
    if choice == 4:
        print 'You have quit the game.'
        loop = 0

I also need to transfer the Function names into numbers for easier input! So that will be done sometime, its not major right now to me.

Still some errors.

Game are looping on one menu.
Give global error on gold.
If you type wrong an exception is trown(NameError)
Try to avoid to use global so much.

Saw you gave opp classes.
That ok,but you can better with your loop-functions design than it is now.

Just a tips on how to design a menu-system with function and loop.

Try to type wrong,you get a warning and get back to menu.
You dont need to put in code to handle exeptions.

def test1():
    '''Info about function'''    
    print 'My code'        
    raw_input("\nTrykk enter for meny")
     
def test2():
    '''Info about function'''    
    print 'My code'  
    raw_input("\nTrykk enter for meny")
    
def menuloop():
    '''Info about function''' 
    while True:               
        print 'Welcome to my menu\n'
        print '(1) Choice 1'
        print '(2) Choice 2' 
        print '(q) Quit' 
        choice = raw_input('Enter your choice: ') 
        if choice == '1':
            test1()
        elif choice == '2': 
            test2() 
        elif choice == 'q': 
            exit() 
        else: 
            print 'Not a correct choice:', choice    

if __name__ == '__main__':
    menuloop()

I am not sure I fully understand, I know if I misstype things it will error, and that global gold does not hurt anything, just a stupid bug I cant get ride of right now..

EDIT: I fixed the global gold problem..

I am not sure I fully understand, I know if I misstype things it will error

If someone misstyping,there are 2 options.
You write in code that handlig excepitons.
Or you try to write code that dont need excepiton handling.
Like i did in my example.
http://docs.python.org/tutorial/errors.html

Sure you can get this to work,but i have to say your code is somewhat messy.
But try to write a game like this you are learing many thing.

Yes, indeed it may be messy, but as you did say..gotta learn some way and some how.. I have a question or 2 about your code bit. Why define the main loop? and what is __name__=='__main__' ?

I do not think I have seen that used..

EDIT: I got the mainmenu to loop if something is not input properly! I just changed my 'input' to raw_input...And making a few quote changes. I also swaped all the 'if' for 1 if and 3 elif's.

if __name__ == '__main__':

It`s a very commen ting to use in pyhon.
You can take it away make no diffrence.
it's in use when you save your program an want to import it.
http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm

Just to make it very clear what it`s dos.
Save my kode as menu.py (menuloop program)

Now copy this file an run it.

import menu  # menu.py

def test(input_name):
    print 'Hi!' ,input_name
    while True:
        a = raw_input('What to play a game? ')
        if a == 'yes':
            menu.menuloop()  # Call my menuloop program          
        else:
            exit()    

test('Roy')

Now it run correct first it run my new code and when a call menuloop() it run that after.

Take away __name__ == '__main__': Run the same.
Now menuloop() start before my new code.
Now you understand what if __name__ == '__main__': dos.

Ok, I will try and read and learn how to use this..No promises tho, but I am sure in time I can learn that little bit. One big question I have is.. I have made a loop for the main menu, and you select different functions from there.. Well, how do I loop a function so I can let them stay in that certian function or choose to exit to the main loop? Take the Shipyard for example.

Ignore the above post, I figured it out...Sometimes I ask before I logically think about it.. lol =)

I also added another Ship type, and refined the menu selection to make it easier. Next I will work on a few small bugs in the input's.

Ok everything syntax wise works fine, but I can not seem to get error handling to work right, If I type anything other than the correct number it comes up NameError: But of course, because I can not figure out a smart way to handle this.. Here is an example 'function' from my program.

choice = input('Type 1, 2: ')
    if choice == 1:
        person = 6
        player = random.randrange(1, 10)


    elif player > person:
        print 'You have robbed someone!'
        gold += random.randrange(20, 150)
        print 'You now have',gold,'Gold'
        Robber()
    elif player < person:
        print 'You was not able to rob this person!'
        gold -= random.randrange(45, 120)
        print 'You got beat down and robbed instead!\n'
        print 'You now have',gold,'Gold\n'
        print 'Better luck next time!\n'
        Robber()

    elif choice == 2:
        print 'Exit!

I had an 'else' statement but that did nothing at all, still errored when anything other than 1 or 2 is input.

EDIT: I used this and it worked, not sure if this is a totally wrong way, but its on python.org.

try:    
    choice = int(raw_input('Type 1, 2: '))
except (TypeError, NameError, ValueError):
    print 'Invalid Input'

I am still having no luck with getting the functions to loop properly and handle errors..Getting to be ridiculous! I have tried everything in my personal knowledge..

Ok I finally worked through this, and got a system that handles the errors I tell it to handle and passes what I tell it to onto the next clause.

try:
        choice = int(raw_input('Enter Number: '))
    except ValueError:
        print 'Invalid Input! Please input only numbers.'
        S()
    else:
        if choice in [1, 2, 3, 4]:

This is just a small snip of it.

So, moving on to the next thingy thing =)

try:
        choice = int(raw_input('Enter Number: '))
    except ValueError:
        print 'Invalid Input! Please input only numbers.'
        S()
    else:
        if choice in [1, 2, 3, 4]:

This is just a small snip of it.

This will lead to strange behaviour and such things due to one problem: the S() function you call to on bad input (adds unwanted recursion). If S is the name of the function that this snippet of code is from, then this may cause odd errors for you in the future.

What's happening is that say you have one call to the S function. In it, someone gives bad input. Upon this, your code makes another call to the S function to get input again. But when this second call is made, your first S call won't be terminated or anything. It pauses until this new S call it makes ends. I.e. each time the user makes bad input, it calls S again, but when that new call is done, it will continue down the script within the original call of S.

This is hard to wrap your head around, so think about it like this:

def S():
    try:
        choice = int(raw_input('Enter Number: '))
    except ValueError:
        print 'Invalid Input! Please input only numbers.'
        # I'M INSERTING THE S FUNCTION CODE WHERE YOU CALLED S() #
        # that new call to S happens here, and this is what S does:
        try:
            choice = int(raw_input('Enter Number: '))
        except ValueError:
            print 'Invalid Input! Please input only numbers.'
            # that new call to S happens here, and this is what S does:
            try:
                choice = int(raw_input('Enter Number: '))
            except ValueError:
                print 'Invalid Input! Please input only numbers.'

As you can see, it gets deeper and deeper into calls to S, but when the last one called ends, it continues along where that one was called from within the previous S call, and so forth back to the first S call. This may cause unexpected behaviour and such.

It's better to loop the input-gathering function in a while loop within the S function. That way you can have it continue looping if there's bad input, but if it gets good input, it can break out and continue down the function.

Can I do a clean up code for the try and except statement such as 'Finally'? I do not plan on Calling the S() function anymore giving that it is a menu type function and is only called by the user, but I still see what you are getting at, so is there a way to do a clean up of it?

Well I'd structure it like this:

# within the function wanting to gather input...
while True:
    try:
        choice = int(raw_input('Enter Number: '))
    except ValueError:
        print 'Invalid Input! Please input only numbers.'
    else:
        break

That loops the whole segment over and over. If there is an error (i.e. a non-numerical input), then it doesn't break out of the loop, leading it to keep asking. If it does get a number as input, it doesn't cause an error so it goes to the else statement, in which I called break. This breaks out of the while loop, and then continues down the script instead of asking again.

When I put it like that it just loops the 'Enter number' part, and dose not let the main menu pop up. I have a Main loop on down the script that loops the main menu... So that disturbs it.

Huh? Can you post some context (code)? This is it within IDLE for me:

>>> while True:
    try:
        choice = int(raw_input('Enter Number: '))
    except ValueError:
        print 'Invalid Input! Please input only numbers.'
    else:
        break

Enter Number: asahrotry
Invalid Input! Please input only numbers.
Enter Number: 1
>>> # it broke out of the while loop fine...

This must just be an error in how you're implementing the code I gave you.

Ahh ya it would work, if I did not have a cluster F$%! of code...Here is what it looks like from THAT function down.

def Tav():
    global gold
    print 'Hello There! Welcome to the Tavern!\n'
    print 'Here you can purchase some beer or liquor!\n'
    print 'You currently have',gold,'Gold\n'

    print '1)Beer 10$'
    print '2)Liquor 20$'
    print '3)Exit\n'
    print 'Enter 1, 2, 3\n'

    try:
        choice = int(raw_input('What will it be? '))
    except ValueError:
        print 'Invalid Input! Please input only numbers.\n'
        Tav()
    else:
        if choice in [1, 2]:
            global price

            if choice == 1:
                price = 10
                if gold >= price:
                    gold -= price
                    print 'You bought a beer!\n'
                    print 'You now have',gold,'Gold\n'
                    Tav()
                elif gold < price:
                    print 'You do not have enough gold!'
                    Tav()

            if choice == 2:
                price = 20
                if gold >= price:
                    gold -= price
                    print 'You bought a shot of liquor!\n'
                    print 'You now have',gold,'Gold\n'
                    Tav()
                elif gold < price:
                    print 'You do not have enough gold!'
                    Tav()

def Gold():
    global gold

def Rep():
    global rep
    global gold
    rep = rep + 1
        
    print 'You have gained more reputation!\n'
    print 'Your Reputation is now:',
    print rep



def main():
    choice = 0
    loop = 1
    while loop == 1:
    
        print 'Welcome to Pirates!\n'
        print 'Please Pick one of the following options!\n'
    
        print 'Type the options as shown!\n'
        print '1)Type: 1 to enter the Shipyard!'
        print '2)Type: 2 to enter the Tavern!'
        print '3)Type: Coming Soon!'
        print '4)Type: Coming Soon!'
        print '5)Type: 5 to exit\n'
        choice = raw_input('Input an option! \n')

        if choice == '1':
            S()

        elif choice == '2':
            Tav()

        elif choice == '5':
            print 'You have quit the game.'
            loop = 0

        else:
            print 'Invalid Input! Please type a Number!'
            
if __name__=='__main__':
    main()

This doesn't even incorporate the suggestion I gave, but still contains the unwanted recursion (see lines 12 to 16, along with 27, 30, 38, 41).

You would have to change the Tav function to:

def Tav():
    global gold
    print 'Hello There! Welcome to the Tavern!\n'
    print 'Here you can purchase some beer or liquor!\n'
    print 'You currently have',gold,'Gold\n'

    print '1)Beer 10$'
    print '2)Liquor 20$'
    print '3)Exit\n'
    print 'Enter 1, 2, 3\n'

    while True:
        try:
            choice = int(raw_input('What will it be? '))
        except ValueError:
            print 'Invalid Input! Please input only numbers.\n'
        else:
            break

    # this now gets executed after "break" is made.
    if choice in [1, 2]:
        # etc...

And even then, you make a couple more calls to the Tav function further down, leading to that recursion thing I told you about. You should be able to figure out a way of changing that :P

Ok I had bad indention is why the it would not work with another loop. But now when I use that 'break' you put after else. It ignores the 'if' statments down below and when I type a number it just ignores it.

I can only assume that you put the indentation level of the if statements so that they were inside the loop. They should be outside of that while loop, like I had in my above code.

Good call...lol

EDIT, well no that might have been one problem but fixing that threw all kinds of errors..damn.. It did not like un- indenting that lol

Ok one problem is where I put 'choice = int raw_input' and so on..
on down I call choice as 'if choice in [1, 2, 3]: it throws a name error choice not defined because its out of the loop that the original choice is in..

Ok, never mind, I un-indented to far. fixed and works right now =)
Thank you Shadwick! If I only had your brain...lol

Ok that loop works but it does not do what is needed, the code I had did exactly what was needed, but as you said, it repeated. That while loop in the Tav() function now screws up the functions above and also dose not loop properly within the function, it just loops the choice, not the whole menu which was the point of the function.. So now I dont know.. I knew what I was doing, when it was my code, but now im lost again...

Well if you wanted it to re-print your menu every time, you'd just need to stick all those print statements from above that while loop to inside it, right on top of that choice = raw_input... part.

As for "messing up the functions above", what exactly do you mean? I can only really answer your questions when you be specific with them, and if there are errors, also show the traceback it gives.

If you wanted Tav to loop the entirety of itself, you can contain its contents within a large while loop, or you could place the call to it from the other part of code within a while loop.

Here is the function above Tav() I want it to do the same as Tav() I set them both up identical but I can not get this one to exit even tho I put a break it still loops..

def S():
    while True:
        global gold
        print 'Welcome to the Shipyard!\n'
        print 'Here you can buy new Ships.\n'
        print 'You currently have',gold,'Gold.\n'
    
    
        Shiplist = {'1)Sloop':'1500$', '2)Schooner':'3500$', '3)Brigantine':'6500$', '4)Galley': '9500$'}
        print 'The current Ships avaliable:\n'
        print Shiplist
        print ' '
        print 'To Exit type 5'
        print 'Type a number to choose the corresponding Ship!\n'
    
    
        try:
            choice = int(raw_input('Enter Number: '))
        except ValueError:
            print 'Invalid Input! Please input only numbers.\n'
        
    
        
        if choice in [1, 2, 3, 4]:
            global price
            global ship
        

        if choice == 1:
            ship = 'Sloop'
            price = 1500

        elif choice == 2:
            ship = 'Schooner'
            price = 3500

        elif choice == 3:
            ship = 'Brigantine'
            price = 6500

        elif choice == 4:
            ship = 'Galley'
            price = 9500
            

        if gold >= price:
            print 'You bought a new ship!'
            gold -= price
            print 'You now have:',
            print gold,
            print 'Gold\n'
                

        elif gold < price:
            print 'You do not have enough Gold!'
            print 'Try again later!\n'
                

        elif choice == 5:
            break

Oh... this code looks fairly um, interesting. There are a number of things that should be changed.

It would be best to indent the lines 2- 57 in one level so that they're under the if that triggers if the input is 1 - 4.

The problem that this should also solve is the "elif choice == 5: break". The issue is that because you put lines 46-56 in that same level as those "choice" ifs, the breaking conditional is now linked as an elif for the "if gold >= price" statement.

Your code should be something like:

# ...
        try:
            choice = int(raw_input('Enter Number: '))
        except ValueError:
            print 'Invalid Input! Please input only numbers.\n'

        if choice in [1, 2, 3, 4]:
            global price
            global ship
            # I indented this below
            if choice == 1:
                ship = 'Sloop'
                price = 1500

            elif choice == 2:
                ship = 'Schooner'
                price = 3500

            elif choice == 3:
                ship = 'Brigantine'
                price = 6500

            elif choice == 4:
                ship = 'Galley'
                price = 9500
                
            if gold >= price:
                print 'You bought a new ship!'
                gold -= price
                print 'You now have:',
                print gold,
                print 'Gold\n'
                    
            elif gold < price:
                print 'You do not have enough Gold!'
                print 'Try again later!\n'
            # ...all the way to here.

        elif choice == 5:
            break

elif links the statement to the most recent if statement on the same level of indentation as it. All that part that I indented only happens if the choice is 1 - 4 anyways, so it should go under that if choice in [1,2,3,4] statement.

The "break" part is now linked as an elif to the if choice in [1,2,3,4] statement because it is the most recent if statement of the same indentation level.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.