Question
3 python programs 1- Write a program that asks for 'name' from the user and then asks for a number and stores the two in
3 python programs
1- Write a program that asks for 'name' from the user and then asks for a number and stores the two in a dictionary (called 'the_dict') as key-value pair. The program then asks if the user wants to enter more data (More data (y/n)? ) and depending on user choice, either asks for another name-number pair or exits and stores the dictionary key, values in a list of tuples and prints the list. Note: Ignore the case where the name is already in the dictionary.
Example:
Name: pranshu
Number: 517-244-2426
More data (y/n)? y
Name: rich
Number: 517-842-5425
More data (y/n)? y
Name: alireza
Number: 517-432-5224
More data (y/n)? n
[('alireza', '517-432-5224'), ('pranshu', '517-244-2426'), ('rich', '517-842-5425')]
This is the skeleton:
dictlist = [] #declare variables #loop needed input("Name: ") input("Number: ") ##add name and number to dictionary input('More data (y/n)? ') #if more data, then repeat
for key, value in the_dict.items(): #we store the dictionary in a list, then sort and print temp = (key,value) dictlist.append(temp) print(sorted(dictlist))
2-
The following function open_file() opens a file called 'example.txt' and returns the file pointer. This function is called within main and the file pointer is used to read lines in the file. Create a dictionary called 'dict_of_words' that have words as 'keys' and (integar) counters as values. The counter values keep a count of the number of times a word has appeared in the text file. In the end, store the dictionary key,values in a list, sort and print the list on the screen.
Note that the counts are not case-sensitive, that is, 'Word' is the same as 'word' or 'wORd'.
Also, note that your progrom should account for if a ',' (comma) separates two words, e.g. 'food, water, electricity'
Example:
Contents of input text file:
I do not think there is any thrill that can go through the human heart like that felt by the inventor as he sees some creation of the brain unfolding to success such emotions make a man forget food sleep friends love everything Nikola Tesla
Output:
[('a', 1), ('any', 1), ('as', 1), ('brain', 1), ('by', 1), ('can', 1), ('creation', 1), ('do', 1), ('emotions', 1), ('everything', 1), ('felt', 1), ('food', 1), ('forget', 1), ('friends', 1), ('go', 1), ('he', 1), ('heart', 1), ('human', 1), ('i', 1), ('inventor', 1), ('is', 1), ('like', 1), ('love', 1), ('make', 1), ('man', 1), ('nikola', 1), ('not', 1), ('of', 1), ('sees', 1), ('sleep', 1), ('some', 1), ('success', 1), ('such', 1), ('tesla', 1), ('that', 2), ('the', 3), ('there', 1), ('think', 1), ('thrill', 1), ('through', 1), ('to', 1), ('unfolding', 1)]
This is the skeleton for 2:
def open_file(): fpointer = open('example.txt') return fpointer
def main(): dictlist = [] fp = open_file() #loop to iterate over lines in file for key, value in dict_of_words.items(): temp = (key,value) dictlist.append(temp) print(sorted(dictlist)) main()
3-
Write 3 functions in the starter code below such that:
add_to_dict(): takes a dictionary, a key, a value and adds the key,value pair to the dictionary. If key is already in dictionary then it displays the error message: "Error. Key already exists.". Returns dictionary.
remove_from_dict(): takes a dictionary and key and removes the key from the dictionary. Returns dictionary. If no such key is found in the dictionary then it prints: "No such key exists in the dictionary.". Hint: Use try-except.
find_key(dictt, key): takes dictionary and key and prints value corresponding to the key from the dictionary: print("Value: ", value). If key is not found, then prints: "Key not found." Hint: Use try-except
Example:
Menu:
add(a), remove(r), find(f): a
Key: rich
Value: 1
More (y/n)? y Menu:
add(a), remove(r), find(f): a
Key: alireza
Value: 2
More (y/n)? n [('alireza', '2'), ('rich', '1')]
Example 2:
Menu:
add(a), remove(r), find(f): a
Key: pranshu
Value: 1
More (y/n)? y Menu:
add(a), remove(r), find(f): r
key to remove: enbody No such key exists in the dictionary.
More (y/n)? n
This is the skeleton for 3:
#add_to_dict() goes here:
#remove_from_dict() goes here
#find_key() goes here
def main(): more = True dictt = {} dictlst = [] while more: print("Menu: ") choice = input("add(a), remove(r), find(f): ") if choice.lower() == "a": key = input("Key: ") value = input("Value: ") dictt = add_to_dict(dictt, key,value) elif choice.lower() == "r": key = input("key to remove: ") dictt = remove_from_dict(dictt,key) elif choice.lower() == "f": key = input("Key to locate: ") find_key(dictt,key) else: print("Invalid choice.") do_more = input("More (y/n)? ") if do_more.lower() != 'y': more = False if dictt: for key, value in dictt.items(): temp = (key,value) dictlst.append(temp) print(sorted(dictlst)) main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started