python - String indices must be integers and while issue -
i writing code school homework. assignment this: write code read book codes till "000" entered , split books 2 categories depending on first 4 digits entered isbn , issn.
note: use str , check prefix of code (in case isbn , issn).
here code:
bookarray = [] booknumber = str(input("give me book code - type 000 if want cancel")) while booknumber != "000": bookarray.append(booknumber) booknumber = str(input("give me book code - type 000 if want cancel")) if booknumber[0,4] == "isbn": isbnarray = booknumber elif booknumber[0,4] == "issn": issnarray = booknumber print(issnarray) print(isbnarray) any regarding same appreciated! in first year in python @ school.
edit: expected output should 2 lists books' code numbers. currently, face autonomous loop on "while" not sure of how , why.
if booknumber[0,4] == "isbn": you're passing tuple index booknumber explains error message ("indices must integers"). want slice:
if booknumber[0:4] == "isbn": in case you'd better off
if booknumber.startswith("isbn"): also, if you're using python 2, entering 000 , passing str(input()) yields "0" since input() evaluates expression , 000 0. it's ok python 3, python 2, switch raw_input() (or ask user enter 0 quit, , test "0" instance. btw input unsafe in python 2).
Comments
Post a Comment