Skip to content Skip to sidebar Skip to footer

Python Text Adventure Selling, Check If Its In List

I'm Coding a Text adventure game. First I coded my inventory player_inventory = [] Since there should be nothing to begin with. and in the shop, when I buy something it would say

Solution 1:

You may want to change the interface design from typing the item's name to a numbered list. The user can select the number of the item to sell. Here is an example function.

def sell(player_inventory):
    print('Available to sell::')
    for i, stuff in enumerate(player_inventory, 1):
        print(i, stuff)
    sold = input('what item number do you want to sell? ("q" to exit)')

    if sold == 'q':
        return player_inventory

    try:
        sold = int(sold)
    except ValueError:
        print('Please use the item number')
        return player_inventory

    if sold <= len(player_inventory):
        print('successfully sold!!')
        player_inventory.pop(sold - 1)
    return player_inventory

Post a Comment for "Python Text Adventure Selling, Check If Its In List"