| | |
Pratice game.
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2009
Posts: 76
Reputation:
Solved Threads: 1
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.
python Syntax (Toggle Plain Text)
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:
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
You would have to change the Tav function to:
python Syntax (Toggle Plain Text)
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...
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
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.
Last edited by shadwickman; Jul 27th, 2009 at 12:40 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
•
•
Join Date: Jul 2009
Posts: 76
Reputation:
Solved Threads: 1
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..
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..
Last edited by Clueless86; Jul 27th, 2009 at 12:47 am.
•
•
Join Date: Jul 2009
Posts: 76
Reputation:
Solved Threads: 1
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.
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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
•
•
Join Date: Jul 2009
Posts: 76
Reputation:
Solved Threads: 1
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..
python Syntax (Toggle Plain Text)
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:
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
The "break" part is now linked as an elif to the
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:
python Syntax (Toggle Plain Text)
# ... 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
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. Last edited by shadwickman; Jul 27th, 2009 at 1:54 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson
my photography
- Hunter S. Thompson
my photography
![]() |
Similar Threads
- looking for C++ London 3D Game Porgrammers - Growing proffessional startup company (Software Development Job Offers)
- Motion Behavior Engineer - C++ - Game Engine (Software Development Job Offers)
- Roblox seeks Game and Graphics Guru (Software Development Job Offers)
- game programmer needed (Software Development Job Offers)
- Ajax Game Developer Needed for a small project (Web Development Job Offers)
- Your Fav Game (Geeks' Lounge)
Other Threads in the Python Forum
- Previous Thread: Subprocess Module Confusion
- Next Thread: Simple Chat Server Problem
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse newb number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable windows write wxpython xlib





