Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python: I need help resolving the error implementing pprint on Exercise 7+8 ################################################################### # Functions ################################################################### import pprint def PromptForInteger(min,max,initialMsg,errorMsg): value=int(input(initialMsg)) while (value < min

Python: I need help resolving the error implementing pprint on Exercise 7+8

################################################################### # Functions ################################################################### import pprint 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 pprint(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 pprint(ncontacts) print(ncontacts.keys()) print(ncontacts.values()) name = input("Whose number do you want? ") if(name in ncontacts.keys()): print(ncontacts[name]) 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 name {}'.format(names[indexName])) copyNames = names[:] print(names) names.remove('Cartman') print(names) print(copyNames) 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(numbers) print(sum(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: ")

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

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions

Question

What is database?

Answered: 1 week ago

Question

What are Mergers ?

Answered: 1 week ago