Home > OS >  if input in list, select next item
if input in list, select next item

Time:10-29

For instance, this is the list:

exchange_list = ['coin', 'quarter', 'dime', 'penny', 'buck', 'bitcoin']

exchanger = input("Please give the input of a money currency that you'd like to exchange")

How do I make it so that if my input for instance is 'penny' that it selects the next item in the list (in this case 'buck'), and if the input is the same as the last item in the list (in this case 'bitcoin'), that it selects the first (in this case 'coin') ?

CodePudding user response:

You can do:

index = (exchange_list.index(exchanger)   1) % len(exchange_list)

print(exchange_list[index])

CodePudding user response:

exchange_list = ['coin', 'quarter', 'dime', 'penny', 'buck', 'bitcoin']

exchanger = str(input("Please give the input of a money currency that you'd like to exchange: "))
foo = exchange_list.index(exchanger)
if foo == len(exchange_list)-1:
    print(exchange_list[0])
else:
     print(exchange_list[foo 1])

CodePudding user response:

exchange_list = ['coin', 'quarter', 'dime', 'penny', 'buck', 'bitcoin']

exchanger = input("Please give the input of a money currency that you'd like to exchange")

location = exchange_list.index(exchanger)
currency = exchange_list[(location 1)%len(exchange_list)]

print(currency)

CodePudding user response:

You can do :

exchange_list = ['coin', 'quarter', 'dime', 'penny', 'buck', 'bitcoin']

exchanger = input("Please give the input of a money currency that you'd like to exchange")

count=0
for item in exchange_list:
    if item==exchanger:   
        if count==len(exchange_list)-1:
            count=-1
        print(exchange_list[count 1]) # You can do other things too
    count=count 1
    
  • Related