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()
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
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.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
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..
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Ok, never mind, I un-indented to far. fixed and works right now =)
Thank you Shadwick! If I only had your brain...lol
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
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...
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
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..
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
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Ok as you said.. I believe.. But It still when option 5 is inputed, it still prints out 'You do not have enough gold' & 'Try again later' statments.. and then loops back to enter the number again.
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, 5]:
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
I know its something stupid I am not seeing, after this im going to bed, because I am nothing thinking clearly..
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Okay, that fixed it..As said, I am going to bed as I am nothing thinking logic but more idiot right now.. So thank you again. and good nite lol
I will update this tomorrow with some more functions I think you might like...I think I understand the 'if' and 'elif' better now in loops.. and also where I use 'choice in'. Good stuff here..And you also help me learn from my stupid mistakes.. Maybe one day I can get them classes down.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
I followed the examples you set last night for 'loops', and things. Today I added a 'Merchant'. So now you can buy a few goods, sell goods you have. the price right now is always the same, I will change this later with a random.randrange so sometimes you may pay more or less, and the items may sell for more or less..
This would rock if it was in a GUI form...maybe not graphics that good, but buttons would be a nice addition sometime..
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
I got pretty much everything lined out except I get an error, here is the error code..
File "C:\Python25\pirates.py", line 35, in S
if choice in [1, 2, 3, 4]:
UnboundLocalError: local variable 'choice' referenced before assignment
Ok I am not sure why its throwing that up, has not done that before, only dose it when I try and make it error by not typing a number.. You can reference my code I have not changed the way its typed or formatted, just exceptions..
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
Do I just need to make it 'except' an UnboundLocalError?
if choice == 1:
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
Well, I thought we had to drop the 'else' statement because I needed all the 'if' and 'elif' functions to be under the loop in order to return to the menu? It was on page 3 when you fixed the indention problem for that to work.. I throw the else statement in there I gotta remove the 'ifs' from the loop in order for it to pass..Which then the menu dose not work.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1
I think we are both on two different pages here..
try:
choice = int(raw_input('Input a Number! '))
except ValueError:
print 'Invalid Input, Please type a Number!\n'
# this must be where it is at!
if choice in [1, 2, 3]:
This above, MUST be in line with the loop/try statement. Otherwise its a waste of time, because it must loop. Doing what you said, puts the 'ifs' outside of the loop, wont work.
Clueless86
Junior Poster in Training
76 posts since Jul 2009
Reputation Points: 10
Solved Threads: 1