Pratice game.

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 76
Reputation: Clueless86 is an unknown quantity at this point 
Solved Threads: 1
Clueless86 Clueless86 is offline Offline
Junior Poster in Training

Re: Pratice game.

 
0
  #21
Jul 27th, 2009
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.
  1. def Tav():
  2. global gold
  3. print 'Hello There! Welcome to the Tavern!\n'
  4. print 'Here you can purchase some beer or liquor!\n'
  5. print 'You currently have',gold,'Gold\n'
  6.  
  7. print '1)Beer 10$'
  8. print '2)Liquor 20$'
  9. print '3)Exit\n'
  10. print 'Enter 1, 2, 3\n'
  11.  
  12. try:
  13. choice = int(raw_input('What will it be? '))
  14. except ValueError:
  15. print 'Invalid Input! Please input only numbers.\n'
  16. Tav()
  17. else:
  18. if choice in [1, 2]:
  19. global price
  20.  
  21. if choice == 1:
  22. price = 10
  23. if gold >= price:
  24. gold -= price
  25. print 'You bought a beer!\n'
  26. print 'You now have',gold,'Gold\n'
  27. Tav()
  28. elif gold < price:
  29. print 'You do not have enough gold!'
  30. Tav()
  31.  
  32. if choice == 2:
  33. price = 20
  34. if gold >= price:
  35. gold -= price
  36. print 'You bought a shot of liquor!\n'
  37. print 'You now have',gold,'Gold\n'
  38. Tav()
  39. elif gold < price:
  40. print 'You do not have enough gold!'
  41. Tav()
  42.  
  43. def Gold():
  44. global gold
  45.  
  46. def Rep():
  47. global rep
  48. global gold
  49. rep = rep + 1
  50.  
  51. print 'You have gained more reputation!\n'
  52. print 'Your Reputation is now:',
  53. print rep
  54.  
  55.  
  56.  
  57. def main():
  58. choice = 0
  59. loop = 1
  60. while loop == 1:
  61.  
  62. print 'Welcome to Pirates!\n'
  63. print 'Please Pick one of the following options!\n'
  64.  
  65. print 'Type the options as shown!\n'
  66. print '1)Type: 1 to enter the Shipyard!'
  67. print '2)Type: 2 to enter the Tavern!'
  68. print '3)Type: Coming Soon!'
  69. print '4)Type: Coming Soon!'
  70. print '5)Type: 5 to exit\n'
  71. choice = raw_input('Input an option! \n')
  72.  
  73. if choice == '1':
  74. S()
  75.  
  76. elif choice == '2':
  77. Tav()
  78.  
  79. elif choice == '5':
  80. print 'You have quit the game.'
  81. loop = 0
  82.  
  83. else:
  84. print 'Invalid Input! Please type a Number!'
  85.  
  86. if __name__=='__main__':
  87. main()
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Pratice game.

 
0
  #22
Jul 27th, 2009
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:
  1. def Tav():
  2. global gold
  3. print 'Hello There! Welcome to the Tavern!\n'
  4. print 'Here you can purchase some beer or liquor!\n'
  5. print 'You currently have',gold,'Gold\n'
  6.  
  7. print '1)Beer 10$'
  8. print '2)Liquor 20$'
  9. print '3)Exit\n'
  10. print 'Enter 1, 2, 3\n'
  11.  
  12. while True:
  13. try:
  14. choice = int(raw_input('What will it be? '))
  15. except ValueError:
  16. print 'Invalid Input! Please input only numbers.\n'
  17. else:
  18. break
  19.  
  20. # this now gets executed after "break" is made.
  21. if choice in [1, 2]:
  22. # 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
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 76
Reputation: Clueless86 is an unknown quantity at this point 
Solved Threads: 1
Clueless86 Clueless86 is offline Offline
Junior Poster in Training

Re: Pratice game.

 
0
  #23
Jul 27th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Pratice game.

 
1
  #24
Jul 27th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 76
Reputation: Clueless86 is an unknown quantity at this point 
Solved Threads: 1
Clueless86 Clueless86 is offline Offline
Junior Poster in Training

Re: Pratice game.

 
0
  #25
Jul 27th, 2009
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..
Last edited by Clueless86; Jul 27th, 2009 at 12:47 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 76
Reputation: Clueless86 is an unknown quantity at this point 
Solved Threads: 1
Clueless86 Clueless86 is offline Offline
Junior Poster in Training

Re: Pratice game.

 
0
  #26
Jul 27th, 2009
Ok, never mind, I un-indented to far. fixed and works right now =)
Thank you Shadwick! If I only had your brain...lol
Last edited by Clueless86; Jul 27th, 2009 at 12:51 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 76
Reputation: Clueless86 is an unknown quantity at this point 
Solved Threads: 1
Clueless86 Clueless86 is offline Offline
Junior Poster in Training

Re: Pratice game.

 
0
  #27
Jul 27th, 2009
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...
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Pratice game.

 
0
  #28
Jul 27th, 2009
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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 76
Reputation: Clueless86 is an unknown quantity at this point 
Solved Threads: 1
Clueless86 Clueless86 is offline Offline
Junior Poster in Training

Re: Pratice game.

 
0
  #29
Jul 27th, 2009
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..
  1. def S():
  2. while True:
  3. global gold
  4. print 'Welcome to the Shipyard!\n'
  5. print 'Here you can buy new Ships.\n'
  6. print 'You currently have',gold,'Gold.\n'
  7.  
  8.  
  9. Shiplist = {'1)Sloop':'1500$', '2)Schooner':'3500$', '3)Brigantine':'6500$', '4)Galley': '9500$'}
  10. print 'The current Ships avaliable:\n'
  11. print Shiplist
  12. print ' '
  13. print 'To Exit type 5'
  14. print 'Type a number to choose the corresponding Ship!\n'
  15.  
  16.  
  17. try:
  18. choice = int(raw_input('Enter Number: '))
  19. except ValueError:
  20. print 'Invalid Input! Please input only numbers.\n'
  21.  
  22.  
  23.  
  24. if choice in [1, 2, 3, 4]:
  25. global price
  26. global ship
  27.  
  28.  
  29. if choice == 1:
  30. ship = 'Sloop'
  31. price = 1500
  32.  
  33. elif choice == 2:
  34. ship = 'Schooner'
  35. price = 3500
  36.  
  37. elif choice == 3:
  38. ship = 'Brigantine'
  39. price = 6500
  40.  
  41. elif choice == 4:
  42. ship = 'Galley'
  43. price = 9500
  44.  
  45.  
  46. if gold >= price:
  47. print 'You bought a new ship!'
  48. gold -= price
  49. print 'You now have:',
  50. print gold,
  51. print 'Gold\n'
  52.  
  53.  
  54. elif gold < price:
  55. print 'You do not have enough Gold!'
  56. print 'Try again later!\n'
  57.  
  58.  
  59. elif choice == 5:
  60. break
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Pratice game.

 
0
  #30
Jul 27th, 2009
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:
  1. # ...
  2. try:
  3. choice = int(raw_input('Enter Number: '))
  4. except ValueError:
  5. print 'Invalid Input! Please input only numbers.\n'
  6.  
  7. if choice in [1, 2, 3, 4]:
  8. global price
  9. global ship
  10. # I indented this below
  11. if choice == 1:
  12. ship = 'Sloop'
  13. price = 1500
  14.  
  15. elif choice == 2:
  16. ship = 'Schooner'
  17. price = 3500
  18.  
  19. elif choice == 3:
  20. ship = 'Brigantine'
  21. price = 6500
  22.  
  23. elif choice == 4:
  24. ship = 'Galley'
  25. price = 9500
  26.  
  27. if gold >= price:
  28. print 'You bought a new ship!'
  29. gold -= price
  30. print 'You now have:',
  31. print gold,
  32. print 'Gold\n'
  33.  
  34. elif gold < price:
  35. print 'You do not have enough Gold!'
  36. print 'Try again later!\n'
  37. # ...all the way to here.
  38.  
  39. elif choice == 5:
  40. 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.
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC