I am a python intermediate and tried to make a text based adventure game in python. In it there is a function called shop() which is executed whenever I type 's'.
buy_item = input('\nWhat would you like to buy? Enter \'n\' to close shop,\'e\' to equip something, \'1\' for dagger, \'2\' for short sword, \'5\' for leather armour and so on. ').lower()
possible_answers = ['1', '2', '3', '4', '5', '6', '7', '8', 'n', 'e']
while buy_item not in possible_answers:
print('Invalid answer')
buy_item = input('What would you like to buy? Enter \'n\' to close shop, \'e\' to equip something, \'1\' for dagger, \'2\' for short sword, \'5\' for leather armour and so on. ').lower()
if buy_item == '1':
if shop_items[0].get('dagger cost') > player['gold']:
print('Not enough gold!')
else:
player['weapons'].append('dagger')
player['gold'] -= shop_items[0].get('dagger cost')
print(f'You just bought dagger for {shop_items[0].get("dagger cost")}')
print(f'Gold: {player["gold"]}')
elif buy_item == 'n':
print('Thank you for visiting the shop. You can always come back by entering \'s\'.')
There is a lot more than that but this should be sufficient to answer this problem. It is working perfectly well but to buy like item 1 and item 2 both, first I will need to type 's', buy item 1 then again type 's' and buy item 2. I want it to stay on the screen until typed 'n' so I wont have to type 's' again and again to buy each item. If more code need then tell me and I will edit this to show full code.
CodePudding user response:
Maybe try:
def shop() -> None:
while True:
buy_item = input('\nWhat would you like to buy? Enter \'n\' to close shop,\'e\' to equip something, \'1\' for dagger, \'2\' for short sword, \'5\' for leather armour and so on. ').lower()
possible_answers = ['1', '2', '3', '4', '5', '6', '7', '8', 'n', 'e']
while buy_item not in possible_answers: # Why are you using 'while'? use if.
print('Invalid answer')
buy_item = input('What would you like to buy? Enter \'n\' to close shop, \'e\' to equip something, \'1\' for dagger, \'2\' for short sword, \'5\' for leather armour and so on. ').lower()
if buy_item == '1':
if shop_items[0].get('dagger cost') > player['gold']:
print('Not enough gold!')
else:
player['weapons'].append('dagger')
player['gold'] -= shop_items[0].get('dagger cost')
print(f'You just bought dagger for {shop_items[0].get("dagger cost")}')
print(f'Gold: {player["gold"]}')
elif buy_item == 'n':
print('Thank you for visiting the shop. You can always come back by entering \'s\'.')
return
(Note: at the start of the shop
, you may need to add global player
as the variable player
will be an undefined variable in the function instance.)
CodePudding user response:
You could declare a variable called 'shopOpen' that would have a boolean value, when the user inputs 's' the variable would be set to True and add a while loop that will keep the shop open while shopOpen == True, once the user inputs 'n' shopOpen would be False the while loop gets exited.
So the code would look something like this:
shopOpen = False
#if statement that checks whether 's' is the input. If input = "s": shopOpen = True
while shopOpen:
buy_item = input('\nWhat would you like to buy? Enter \'n\' to close shop,\'e\' to equip something, \'1\' for dagger, \'2\' for short sword, \'5\' for leather armour and so on. ').lower()
possible_answers = ['1', '2', '3', '4', '5', '6', '7', '8', 'n', 'e']
while buy_item not in possible_answers:
print('Invalid answer')
buy_item = input('What would you like to buy? Enter \'n\' to close shop, \'e\' to equip something, \'1\' for dagger, \'2\' for short sword, \'5\' for leather armour and so on. ').lower()
if buy_item == '1':
if shop_items[0].get('dagger cost') > player['gold']:
print('Not enough gold!')
else:
player['weapons'].append('dagger')
player['gold'] -= shop_items[0].get('dagger cost')
print(f'You just bought dagger for {shop_items[0].get("dagger cost")}')
print(f'Gold: {player["gold"]}')
elif buy_item == 'n':
print('Thank you for visiting the shop. You can always come back by entering \'s\'.')
shopOpen = False