Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(+5)Ask the user for a string and creates the following dictionary: The values are the letters in the string, with the corresponding key being the

(+5)Ask the user for a string and creates the following dictionary: The values are the letters in the string, with the corresponding key being the place in the string. For example if the user entered the string CSC120 then create the dictionary d

d = {'0':'C', 1:'S', 2:'C', 3:'1', 4:'2', 5:'0'}

(+5) Display the length of the dictionary d using a len function In this case the answer is 6

(+5) Ask the user for a key(integer). If the key is in the dictionary display the corresponding value. As an example if the user entered 2 display C if the user entered a key that was not in the dictionary display Error key not found

(+5) Ask the user for a value (one of the characters in our example). If the value is in the dictionary display Value found otherwise display Value not found. As an example if the user entered C display Value found

(+5) Ask the user for a character and add it to the dictionary. As an example, if the user enters X then the key should be the next key integer (in this case 6 but in general it should be related to the length of the dictionary ) For this example

d = {'0':'C', 1:'S', 2:'C', 3:'1', 4:'2', 5:'0', 6:X}

see the code to add the name Hopper to the dictionary in the Lab09Template code below

Display the dictionary

(+15) Ask the user for a string of digits, separated by a comma. Print out the corresponding values for those digits.

For example if the user enters the string 0, 1, 4 then print out CS2 At issue is how to handle digits that exceed the range of key values for example, in our case, any digits in the range 05 should generate a corresponding value but any digit(s) > 5 OR < 0 has to be handled appropriately -- I leave that to your decision ( generate an error message or ignore the values) See the last part of the Lab09 template code for an example, on how to handle the string 11, 0, 2 (11 is not a key)

#lab09Template.py names = { 10:"Euclid", 20:"Archimedes", 30:"Newton", 40:"Descartes", 50:"Fermat", 60:"Turing", 70:"Euler", 80:"Einstein", 90:"Boole", 100:"Fibonacci", 110:"Nash" } print("1 ",names.keys()) # print keys print("2 ", names.values() ) # insert Hopper with a key == 120 names[120] = "Hopper" print("3 names == ", names) print(' the length of names is ', len(names) ) #use in operator to search for keys as well as non-existence keys print('searching...') key = 110 if key in names.keys(): print("4 key == ", key, ' value == ',names[key]) else: print("error key == ', key") s = "Euler" # search for Euler if s in names.values(): print(" 5 value ", s," found ") else: print("NO ", s) print("====ALL KEYS====") for key in names: print(key, names[key]) print("==== ALL VALUES====") for values in names.values(): print(values) print("====string manipulation====") # isolating individuals integers using the split() and int() functions s = (input("Enter several integers separated by a comma ")) # NOTE cannot convert 110,20,3 into an integer directly print(' 6 s ==' , s) s = s.split(',') # now split using , as a separator, into individual words print('7 using the split function ',s, 'length == ', len(s) ) # still a string but separated into words # now convert position 0 in the string to an integers print(' 8 int (s[0]) == ', int(s[0]) ) # now print it if int(s[0] ) in names.keys(): # check if the keys values print("9 key found == ", key, ' value == ',names[key]) else: print('ERROR key == ', int(s[0]), 'NOT in key range ') # now the second .... we may need a loop for this !!!!!! x = int(s[1]) # now convert position 1 to a string print('10 x == ', x) # now print it # we need to check if int(s[1]) is with th range of key values if x in names.keys(): print("11 key found == ", x , ' value == ',names[x]) else: print('error key == ', int(s[1]), 'NOT in key range ') for i in s: print( i) # displays all the keys check to see in dictionary 

3 names =={10: 'Euclid', 20: 'Archimedes', 30: 'Newton', 40: 'Descartes', 50: 'Fermat', 60: 'Turing', 70: 'Euler', 80: 'Einstein', 90: 'Boole', 100: 'Fibonacci', 110: 'Nash', 120: 'Hopper'}

the length of names is12

4 key ==110value ==Nash

5 valueEulerfound

====ALL KEYS====

10 Euclid

20 Archimedes

30 Newton

40 Descartes

50 Fermat

60 Turing

70 Euler

80 Einstein

90 Boole

100 Fibonacci

110 Nash

120 Hopper

==== ALL VALUES====

====string manipulation====

Enter several integers separated by a comma

6 s == 11, 120, 40

7 using the split function['11', ' 120', ' 40'] length ==3

8 int (s[0]) ==11

ERROR key ==11 NOT in key range

10 x ==120

11 key found ==120value ==Hopper

Process finished with exit code 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions