To make the original code work as intended, I believe you also need to convert the user input to int
type and subtract 1 because you specified start=1
when printing the options.
market= {'addresses': ["This Street", "That Street"]}
print("Which house do you want to sell")
for number,address in enumerate(market['addresses'], 1):
print(number, '->', address)
userSell = int(input("> ")) - 1
if userSell in range(len(market['addresses'])):
print(f"Sold {address}")
else:
print("Address not found...")
Having to subtract 1 to make the input match the options does not feel very robust as you will have to remember to adjust the index any time you look up elements in the list later in the code. It is a good idea to store the numbered options and reference the same object later. Converting to a dict
makes the code nice and readable and understandable.
market= {'addresses': ["This Street", "That Street"]}
numbered_options = dict(enumerate(market['addresses'], 1))
print("Which house do you want to sell")
for number,address in numbered_options.items():
print(number, '->', address)
userSell = int(input("> "))
if userSell in numbered_options:
print(f"Sold {numbered_options[userSell]}")
else:
print("Address not found...")
What I would really recommend, though, is using one of the many pre-built libraries for displaying lists of options and getting choices from users in the command line. You can choose from the ones mentioned in this thread and consult library documentation to implement them. No need to redo something somebody else has already done for you.