0

The question is basically asking me to write a two-dimensional array that stores a band name and the number of sales they have. Then it asks me to write a program that sorts the list using bubble sort in ascending order according to the number of sales. The first two parts are fairly simple and are working fine. However, it later asks me to allow it to let the user enter a name of a band and be given their chart position using a linear search. This is the part I am struggling with.

myList = [[53,"band1"],[26,"band2"],[923,"band3"],[31,"band4"],[44,"band5"],
[55,"band6"],[231,"band7"]]
def bubbleSort(myList):
    for passnum in range(len(myList)-1,0,-1):
        for i in range(passnum):
            if myList [i]>myList[i+1]:
                top7 = myList[i]
                myList[i] = myList[i+1]
                myList[i+1] = top7

x = int(input("Enter a band name: "))

found = False

for i in range(len(myList)):
 if(myList[i] == x):
  found = True
  print("%d found at %dth position"%(x,i))
  break

if(found == False):
 print("%d is not in list"%x)


bubbleSort(myList)
print(myList)

this is the output: Enter a band name: band4 Traceback (most recent call last): File "python", line 10, in ValueError: invalid literal for int() with base 10: 'band4'

Kevon
  • 1
  • 2

1 Answers1

0

Why are you using int(input()) when you need a string for the band name?

x=input("Enter band name") 

Should work

Ashwath Narayan
  • 143
  • 1
  • 1
  • 10
  • Thanks for the suggestion. However now I get this error Traceback (most recent call last): File "python", line 21, in TypeError: %d format: a number is required, not str – Kevon Jan 30 '18 at 22:07
  • Replace the first %d with %s as x is a string and not an integer. – Ashwath Narayan Jan 30 '18 at 22:10
  • That seems to have fixed the syntax error however the code says that the band is not in the list when it actually is. "Enter band name band2 band2 is not in list" However band2 is in the list – Kevon Jan 30 '18 at 22:24
  • Yeah I've tried every combination and it's still saying that the band is not in the list – Kevon Jan 30 '18 at 23:22