номер заказа отличается от номера индекса - PullRequest
0 голосов
/ 02 июня 2018

У меня есть такая ситуация, когда мне нужно выбрать слово, чтобы программа выводила значение слова, но номер заказа не соответствует номеру индекса x, пример (1,2,3), но индексы (0, 3,7), и в следующий раз это может быть (1,2,3) и индекс (1,3,5), не могли бы вы помочь, как я могу решить эту проблему.Я хочу любое решение, если выбрать второе слово, я хочу его значение.спасибо с уважением umer selmani

empt_list=[]
    empt_list_meaning=[]
    def game():
            empt_dict = dict(zip(empt_list, empt_list_meaning))
            a_options = input("Please select one of these options: ")
            if a_options == 1:
                a_newword = str(raw_input("What word you want to add? "))
                empt_list.append(a_newword)
                a_newword_meaning=str(raw_input("add the meaning of the word: "))
                empt_list_meaning.append(a_newword_meaning)

            elif a_options == 2:
                a_select_word = raw_input("select the words, you want")
                zero = 0
                for word in empt_dict:
                    if a_select_word in word:
                        zero += 1
                        print zero, word,
                        print empt_dict.keys().index(word)

            print ("would you like to continue or exit?\n1.contine\n2.exit")
            now = input(">>> ")
            if now == 1:
                game()
            else:
                print "bye"

game()

здесь, выходной.

"C:\Users\Umer Selmani\venv\Scripts\python.exe" "C:/Users/Umer Selmani/.PyCharm2017.3/config/scratches/scratch_10.py"
Please select one of these options: 1
What word you want to add? ball
add the meaning of the word: round shape item
would you like to continue or exit?
1.contine
2.exit
>>> 1
Please select one of these options: 1
What word you want to add? cactus
add the meaning of the word: flower type
would you like to continue or exit?
1.contine
2.exit
>>> 1
Please select one of these options: 1
What word you want to add? fall
add the meaning of the word: season
would you like to continue or exit?
1.contine
2.exit
>>> 1
Please select one of these options: 2
select the words, you wantll
1 ball 0
2 fall 2
would you like to continue or exit?
1.contine
2.exit
>>> 2
bye

1 Ответ

0 голосов
/ 02 июня 2018

Как прокомментировали другие, словари не упорядочены по умолчанию в Python 2. Единственная реальная проблема с вашим кодом - это первая строка в игровой функции.Попробуйте что-то вроде этого:

empt_list=[]
empt_list_meaning=[]
def game():
        # Dict not needed
        #empt_dict = dict(zip(empt_list, empt_list_meaning))
        # add clarification of what the options are
        print 'Press 1 to add a word or 2 to search for a word.'
        a_options = input("Please select one of these options: ")
        if a_options == 1:
            a_newword = str(raw_input("What word you want to add? "))
            empt_list.append(a_newword)
            a_newword_meaning=str(raw_input("add the meaning of the word: "))
            empt_list_meaning.append(a_newword_meaning)

        elif a_options == 2:
            a_select_word = raw_input("select the words, you want")
            # Use enumerate instead of variable here
            #zero = 0
            for zero, word in enumerate(empt_list):
                if a_select_word in word:
                    #zero += 1
                    # Index the lists you already have instead of indexing the keys of an unordered dict
                    print zero, word, empt_list_meaning.index(word)
                    print empt_list_meaning[zero]

        print ("would you like to continue or exit?\n1.contine\n2.exit")
        now = input(">>> ")
        if now == 1:
            game()
        else:
            print "bye"
game()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...