Question
Python: Need help resoloving error in function AddNewFullContact(ncontacts) and output with exercise 5-8: My Code: ################################################################### # Functions ################################################################### def PromptForInteger(min,max,initialMsg,errorMsg): value=int(input(initialMsg)) while (value <
Python: Need help resoloving error in function AddNewFullContact(ncontacts) and output with exercise 5-8:
My Code:
################################################################### # Functions ################################################################### def PromptForInteger(min,max,initialMsg,errorMsg): value=int(input(initialMsg)) while (value< min or value>max): value=int(input(errorMsg))
return value
def AddNewContact(contacts): for i in range(5): name = input("Please enter the name of your friend: ") number = PromptForInteger(1000000,9999999,"What is your friend's 7-digit phone number? ","That's not 7 digits, try again: ") contacts[name] = number print(contacts) print(contacts.keys()) print(contacts.values()) name = input("Whose number do you want? ") if(name in contacts.keys()): print(contacts[name]) else: print("Name not found!")
def AddNewFullContact(ncontacts): for i in range(5): name = input('Please enter the name of your friend:') number = PromptForInteger(1000000,9999999,"What is your friend's 7-digit phone number? ","That's not 7 digits, try again: ") email = input("What is your friend's email address?") age = PromptForInteger(0,125,"What is your friend's age?","That's not a real age, try again: ") temp = {'number':number,'age':age,'email':email} ncontacts[name]=temp print({ncontacts}) print(ncontacts.keys()) print(ncontacts.values()) name = input("Whose number do you want? ") if(name in nncontacts.keys()): print(ncontacts[name][email]) else: print("Name not found!")
################################################################### # Main ###################################################################
exercise = -1 while (exercise != 0): exercise = int(input("Which exercise would you like to test (1-8, 0 to exit)? "))
##################################################### # Exercise 1: Prompt for Integer ##################################################### if exercise == 1: print("Running exercise " + str(exercise)) # Now you can call function print(PromptForInteger(1,100,"Please enter an integer (1 to 100): ","Your response must be from 1 to 100, try again: "))
##################################################### # Exercise 2: Custom Range ##################################################### elif exercise == 2: print("Running exercise " + str(exercise)) # Function definition is here # Now you can call function print(PromptForInteger(-50,50,"Please enter an integer (-50 to 50):","Your response must be from -50 to 50, try again: "))
print(PromptForInteger(98, 106, "Please enter your temperature (98 to 106):","You must be dead, try again (98 to 106): "))
print(PromptForInteger(-25, -1, "Please enter the deduction from your account (-25 to -1):","Your response must be from -25 to -1, try again: "))
print(PromptForInteger(1, 10, "Please enter a menu selection (1 to 10):","You must choose 1 to 10, try again: ")) ##################################################### # Exercise 3: Modifying the Exercise Loop ##################################################### elif exercise == 3: print("Running exercise " + str(exercise))
##################################################### # Exercise 4: Simple Lists ##################################################### elif exercise == 4: print("Running exercise " + str(exercise)) items =["Wallet","Phone","Keys"] print(items) items.sort() print(items) print(items[0]) print(items[1:]) print(items[-1]) print(items.index("Keys")) items.append("Tablet") print(items) items.insert(1, "Glasses") print(items) items.remove("Phone") print(items) items.reverse() print(items)
##################################################### # Exercise 5: Display List Elements on Demand and Copying Lists ##################################################### elif exercise == 5: print("Running exercise " + str(exercise)) names = ["Stan", "Cartman", "Butters", "Kyle", "Kenny"] for index,name in enumerate(names): print(index,name) indexName = PromptForInteger(0,4,"Choose a name from the list by number: ","You must choose one of the above numbers, try again: ") print('You chose the name {}'.format(names[indexName])) copyNames = names.copy() print(copyNames) names.remove('Cartman') print(names) copyNames.remove('Kenny') print(names) print(copyNames)
##################################################### # Exercise 6: Summation ##################################################### elif exercise == 6: print("Running exercise " + str(exercise)) numbers = [] for i in range(10): x = PromptForInteger(1,100,"Please enter an integer (1 to 100): ","Your response must be from 1 to 100, try again: ") numbers.append(x) print(sum(numbers)) print(numbers)
##################################################### # Exercise 7: Simple Dictionary ##################################################### elif exercise == 7: print("Running exercise " + str(exercise)) contacts={} AddNewContact(contacts)
##################################################### # Exercise 8: Nested Dictionaries ##################################################### elif exercise == 8: print("Running exercise " + str(exercise)) ncontacts={} AddNewFullContact(ncontacts)
##################################################### # Invalid choice ##################################################### elif exercise == 0: exit
else: print("Your response must be from 0 to 8, try again: ")
Exercise 5 Expected Output:
Which exercise would you like to test (1-8, 0 to exit)? Running exercise 5 0 Stan 1 Cartman 2 Butters 3 Kyle 4 Kenny Choose a name from the list by number: You chose name Stan ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle'] Which exercise would you like to test (1-8, 0 to exit)? Running exercise 5 0 Stan 1 Cartman 2 Butters 3 Kyle 4 Kenny Choose a name from the list by number: You chose name Cartman ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle'] Which exercise would you like to test (1-8, 0 to exit)? Running exercise 5 0 Stan 1 Cartman 2 Butters 3 Kyle 4 Kenny Choose a name from the list by number: You chose name Butters ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle'] Which exercise would you like to test (1-8, 0 to exit)? Running exercise 5 0 Stan 1 Cartman 2 Butters 3 Kyle 4 Kenny Choose a name from the list by number: You chose name Kyle ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle'] Which exercise would you like to test (1-8, 0 to exit)? Running exercise 5 0 Stan 1 Cartman 2 Butters 3 Kyle 4 Kenny Choose a name from the list by number: You chose name Kenny ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Butters', 'Kyle', 'Kenny'] ['Stan', 'Cartman', 'Butters', 'Kyle'] Which exercise would you like to test (1-8, 0 to exit)?
Exercise 6 Expected Output:
Which exercise would you like to test (1-8, 0 to exit)? Running exercise 6 Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 55 Which exercise would you like to test (1-8, 0 to exit)? Running exercise 6 Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] 55 Which exercise would you like to test (1-8, 0 to exit)? Running exercise 6 Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): Please enter an integer (1 to 100): [10, 10, 10, 10, 10, 10, 10, 10, 10, 10] 100 Which exercise would you like to test (1-8, 0 to exit)?
Exercise 7 Expected Output:
Which exercise would you like to test (1-8, 0 to exit)? Running exercise 7 Please enter the name of your friend: What is your friend's 7-digit phone number? Please enter the name of your friend: What is your friend's 7-digit phone number? Please enter the name of your friend: What is your friend's 7-digit phone number? Please enter the name of your friend: What is your friend's 7-digit phone number? Please enter the name of your friend: What is your friend's 7-digit phone number? {'Butters': 3456789, 'Cartman': 2345678, 'Kenny': 9876543, 'Kyle': 5678980, 'Stan': 1234567} dict_keys(['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny']) dict_values([1234567, 2345678, 3456789, 5678980, 9876543]) Whose number do you want? 2345678 Which exercise would you like to test (1-8, 0 to exit)? Exercise 8 Expected Output:
Which exercise would you like to test (1-8, 0 to exit)? Running exercise 8 Please enter the name of your friend: What is your friend's 7-digit phone number? What is your friend's email address? What is your friend's age? Please enter the name of your friend: What is your friend's 7-digit phone number? What is your friend's email address? What is your friend's age? Please enter the name of your friend: What is your friend's 7-digit phone number? What is your friend's email address? What is your friend's age? Please enter the name of your friend: What is your friend's 7-digit phone number? What is your friend's email address? What is your friend's age? Please enter the name of your friend: What is your friend's 7-digit phone number? What is your friend's email address? What is your friend's age? {'Butters': {'age': 9, 'email': 'butterman@whatwhat.what', 'number': 3456789}, 'Cartman': {'age': 11, 'email': 'cartman@fatman.eu', 'number': 2345678}, 'Kenny': {'age': 10, 'email': 'theykilledkenny@omg.omg', 'number': 9876543}, 'Kyle': {'age': 10, 'email': 'kylieb@brov.ski', 'number': 5678980}, 'Stan': {'age': 10, 'email': 'stan@stan.com', 'number': 1234567}} dict_keys(['Stan', 'Cartman', 'Butters', 'Kyle', 'Kenny']) dict_values([{'number': 1234567, 'email': 'stan@stan.com', 'age': 10}, {'number': 2345678, 'email': 'cartman@fatman.eu', 'age': 11}, {'number': 3456789, 'email': 'butterman@whatwhat.what', 'age': 9}, {'number': 5678980, 'email': 'kylieb@brov.ski', 'age': 10}, {'number': 9876543, 'email': 'theykilledkenny@omg.omg', 'age': 10}]) Whose number do you want? {'age': 10, 'email': 'kylieb@brov.ski', 'number': 5678980} kylieb@brov.ski Which exercise would you like to test (1-8, 0 to exit)?
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