-1

this is for my code function code

def edit():
    i()
    indeks = input(" masukan hobi ke :")
    if int(indeks)>len(hobi):
        print("data tidak ada ")
    elif int (indeks) <= len(hobi):
        hobibaru=input("hobi baru :)
        hobi[indeks]=hobibaru

this is my error comm

Traceback (most recent call last): File "C:\Users\murtadho\AppData\Local\Programs\Python\Python37\x.py", line 74, in show_menu() File "C:\Users\murtadho\AppData\Local\Programs\Python\Python37\x.py", line 62, in show_menu edit_data() File "C:\Users\murtadho\AppData\Local\Programs\Python\Python37\x.py", line 33, in edit_data buku[indeks] = judul_baru TypeError: list indices must be integers or slices, not str

daffa faiz
  • 27
  • 3

3 Answers3

2

IIUC try:

def edit():
    i()
    indeks = input(" masukan hobi ke :")
    if int(indeks)>len(hobi):
        print("data tidak ada ")
    elif int (indeks) <= len(hobi):
        hobibaru=input("hobi baru :")
        hobi[int(indeks)]=hobibaru

I can't guarantee that it would work, but it sounds reasonable and I expect it to work.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • @daffafaiz I simply change `indeks` on the line `hobi[indeks]=hobibaru` to an integer using `int(...)`, so it becomes: `hobi[int(indeks)]=hobibaru`, and that's it, please accept it – U13-Forward Jun 25 '19 at 03:21
0

There seems to be a couple of issues:

You're getting input and trying to typecast it into an int. While you maybe sure of it being an integer, it may lead towards issues if a string is inserted.

def edit():
    i()
    indeks = input(" masukan hobi ke :")
    try:
        indeks = int(indeks)
    except:
        break
    if indeks >len(hobi):
        print("data tidak ada ")
    elif indeks <= len(hobi):
        hobibaru=input("hobi baru :")
        hobi[indeks]=hobibaru
user1449249
  • 196
  • 10
0

I suggest you should return your input indeks to int before work with list

def edit():
    i()
    indeks = int(input(" masukan hobi ke :"))
    if indeks>len(hobi):
        print("data tidak ada ")
    elif indeks <= len(hobi):
        hobibaru=input("hobi baru :)
        hobi[indeks]=hobibaru
Lê Tư Thành
  • 1,063
  • 2
  • 10
  • 19