2 метода, но пока не удалось решить.Python 2.7 - PullRequest
0 голосов
/ 28 мая 2018

Я пытался:

Сначала я сделал код, подобный этому

elif a_options == 2:
        a_update=raw_input("select a word to update:")
        a_renew_word = raw_input("the new word")
        if a_update in empt_list:
            empt_list[a_update]=a_renew_word
        else:
            print "sorry"
        # print a_list[a_options-1]

получил ошибку:

TypeError: list indices must be integers, not str

Также пробовал:

elif a_options == 2:
    a_update=raw_input("select a word to update:")
    a_renew_word = raw_input("the new word")
    if a_update in empt_list:
        empt_list.index(a_update)=a_renew_word
    else:
        print "sorry"

Другая ошибка

SyntaxError: can't assign to function call

здесь вы идете с полным кодом, я пыталсяделать со списками, потому что я думал, что это подходит лучше всего, но если есть какой-либо другой метод, я приму его.Любая идея, или найти, где находится проблема, которую я пытался найти, но я не мог найти никакого решения, здесь я здесь, чтобы спросить вас.

print "hello, welcome to"
a_list=["1. Add a new word","2. Update and existing word","3. Detele and existing word","4. Display a words definition"]
zero=0
empt_list=[]
empt_list_meaning=[]
def list_game():
    for i in a_list:
        print i
    a_options=input("Please select one of these options: ")
    if a_options== 1:
        a_newword=raw_input("What word you want to add? ")
        empt_list.append(a_newword)
        a_newword_meaning=raw_input("add the meaning of the word")
        empt_list_meaning.append(a_newword_meaning)
        # print a_list[a_options-1]
        print empt_list,a_newword,"added correctly"

    elif a_options == 2:
        a_update=raw_input("select a word to update:")
        a_renew_word = raw_input("the new word")
        if a_update in empt_list:
            empt_list.index(a_update)=a_renew_word
        else:
            print "sorry"
        # print a_list[a_options-1]

    elif a_options == 3:
        a_del_word=raw_input("selct the word you want to delete")
        for i in empt_list:
            if a_del_word in empt_list:
                empt_list.remove(a_del_word)
        # print a_list [a_options-1]


    elif a_options  == 4:
        for i in empt_list:
            print i
    print ("would you like to continue or exit?\n1.contine\n2.exit")
    now=input(">>> ")
    if now==1:
        list_game()
    else:
        print "arrivederchi"
list_game()

Я не знаю, что еще я могу сделать, ценю любойвид помощи.спасибо с уважением umer selmani

1 Ответ

0 голосов
/ 28 мая 2018

Используйте .index(), чтобы найти позицию, затем присвойте списку в этой позиции:

a_update=raw_input("select a word to update:")
a_renew_word = raw_input("the new word")
if a_update in empt_list:
    position = empt_list.index(a_update)
    empt_list[position] = a_renew_word
else:
    print "sorry"

Это то же самое, что предложил @AChampion, но разбито на две отдельные строки, чтобы сделать егонемного понятнее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...