Advertisement
Andreas_M

lists

Sep 30th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. def main():
  4.     preparedList = [] #Gör list för ord
  5.     dictionaryList = [] #Gör list för beskrivning
  6.     while 1 == 1:
  7.         print "Menu for dictionary"
  8.         print "1: Insert"
  9.         print "2: Lookup"
  10.         print "3: Delete a word"
  11.         print "4: Exit program"
  12.         menyval = raw_input("Choose alternative(posetive integer): ")
  13.         if menyval=="1":
  14.             insert(preparedList, dictionaryList)
  15.         elif menyval=="2":
  16.             lookup(preparedList, dictionaryList)
  17.         elif menyval=="3":
  18.             deleteaword(preparedList, dictionaryList)
  19.         elif menyval=="4":
  20.             break #avsulta loopen.
  21.         else:
  22.             print "****"
  23.             print "You didnt type in a number in the right range (1-4)"
  24.             print "****"
  25.  
  26. def insert(preparedList, dictionaryList):
  27.     insertWord = raw_input("Word to insert: ").lower()
  28.     insertExp = raw_input("Describe the item: ").lower()
  29.     if insertWord.lower() in preparedList: #Kolla ifall ordet redan finns
  30.         print "****"
  31.         print "That word already exists, try again."
  32.         print "****"
  33.     elif insertWord.isspace() or insertExp.isspace(): #Kolla ifall det bara är spaces.
  34.         print "****"
  35.         print "You need to write something, not just spaces."
  36.         print "****"
  37.     elif len(insertWord) == 0 or (insertExp) == 0: #kolla så användaren inte bara trycker enter
  38.         print "****"
  39.         print "You need to write something, not just enter or a single character.."
  40.         print "****"
  41.     else:
  42.         preparedList.append(insertWord) #Lägg till ord i preparedList
  43.         dictionaryList.append(insertExp) #Lägg till beskrivning i dictionaryList
  44.         print "----"
  45.         print "Sucessfully inserted", insertWord, "into the wordlist"
  46.         print "----"
  47.     return
  48.  
  49. def lookup(preparedList, dictionaryList):
  50.     searchedWord =  raw_input("What word do you want to search for?: ").lower()
  51.     if searchedWord in preparedList: #Sök igenom preparedList för ordet först.
  52.         getIndex = preparedList.index(searchedWord) #index av vart ordet som söks ligger (int)
  53.         print "----"
  54.         print "Descrition for",searchedWord,":",dictionaryList[getIndex] #använd getIndex för att få rätt plats på beskrivning
  55.         print "----"
  56.     else:
  57.         print "****"
  58.         print "That word does not exist."
  59.         print "****"
  60.  
  61. def deleteaword(preparedList, dictionaryList):
  62.  
  63.     print "These are the words in your wordlist: "
  64.     print "----"
  65.     for item in preparedList: #printa ut alla orden som finns i preparedList
  66.         print item,
  67.     print ""
  68.     print "----"
  69.     deleteWord = raw_input("What word would you like to delete: ").lower()
  70.     if deleteWord in preparedList: #kolla ifall ordet finns i preparedList
  71.         getIndex = preparedList.index(deleteWord) #Ännu en gång, ta index för vart ordet finns i sin lista
  72.         preparedList.remove(deleteWord) #ifall det finns, delete:a det.
  73.         del dictionaryList[getIndex] #använd getIndex för att deleta den rätta beskrivningen.
  74.         print "----"
  75.         print "Sucessfully deleted", deleteWord, "from the wordlist"
  76.         print "----"
  77.     else:
  78.         print "****"
  79.         print "That word doesnt exist in our wordlist."
  80.         print "****"
  81.     return
  82.  
  83. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement