1

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.

2 Answers 2

1

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

It worked for some time but suddenly it is not working
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.