Question: Python: Exercise 1: Prompt for Integer Function The goal of this function is to repeatedly ask the user for an integer until one is received
Python:
Exercise 1: Prompt for Integer Function
The goal of this function is to repeatedly ask the user for an integer until one is received that is within the 1 -> 100 range (inclusively). The strings you need to print have been provided for you.
- Create a function called PromptForInteger in the "Functions" area of the provided code - while python allows you to create functions just about anywhere in your code, it is good style to put all of your functions at the top of your code so that they are easy to find
- The function should return one value - a valid integer (between 1 and 100) that the user entered
- Inside the function, ask the user to enter an integer between 1 and 100 by displaying:
- "Please enter an integer (1 to 100): "
- Use the input() function here and provide the string as its parameter
- Read in the input and store it in a variable
- While the user enters invalid values (less than 1 or greater than 100)
- Print the error "Your response must be from 1 to 100, try again: "
- Use the input() function here and provide the error message string as its parameter
- Once the user enters a valid value, return that value
- Call the function in the area for Exercise 1 and print the value it returns
Run and test your code for this exercise. Evaluate your code and it should pass the tests for this exercise.
Exercise 2: Custom Range
- Modify your PromptForInteger to have four parameters:
- a minimum value for the integer
- a maximum value for the integer
- a string that represents the initial message printed
- a string that represents the error message printed if an invalid response is received
- Modify your loop in the function to use the minimum and maximum values
- Modify your prompts in the function to use the parameters
- Modify exercise 1 to use this new version of the function
- Call the function four times in the area for Exercise 2 and print the value it returns. Use the following min and max values and messages:
- -50, 50, "Please enter an integer (-50 to 50): ", "Your response must be from -50 to 50, try again: "
- 98, 106, "Please enter your temperature (98 to 106): ", "You must be dead, try again (98 to 106): "
- -25, -1, "Please enter the deduction from your account (-25 to -1): ", "Your response must be from -25 to -1, try again: "
- 1, 10, "Please enter a menu selection (1 to 10): ", "You must choose 1 to 10, try again: "
Exercise 3: Modifying the Exercise Loop
- Modify the while loop that loops through the exercises to use your PromptForInteger method
- Use the initial prompt and the appropriate error from the 'else' statement as arguments
- There is no need to modify the Exercise 3 area
- This will change the functionality of this system just a little, but the previous exercises should still work!
Exercise 4: Simple Lists
- Create a list with the words "Wallet", "Phone", and "Keys" in it
- Print the list using a single line of code
- Sort the list using sort
- Print the list again
- Print the first item in the list
- Print everything except the first item in the list (hint: slicing...) as a list
- Print the last item in the list (hint: negative index)
- Print the index of "Keys" (hint: index() function)
- Append "Tablet" to the list
- Print the list
- Insert "Glasses" to the list as the second item in the list
- Print the list
- Remove "Phone" from the list
- Print the list
- Reverse the list
- Print the list
Exercise 5: Display List Elements on Demand and Copying Lists
- Create a list of names: "Stan", "Cartman", "Butters", "Kyle", "Kenny"
- Use enumerate() in a loop to display the index of each name followed by a space, followed by the name, on a line by itself, like so:
- "# name"
- replace # with the index and name with the actual name from the list
- ex:
- 0 Stan
- 1 Cartman
- 2 Butters
- Using PromptForInteger, ask the user for the index of an item in the list (hint: base the maximum on the size of the list. what would be the minimum?)
- message: "Choose a name from the list by number: "
- error: "You must choose one of the above numbers, try again: "
- Print the name the user selects
- "You chose name X" where X is replaced by the name
- Make a copy of the list of names
- Print the copy
- Remove Cartman from the original list
- Print the original list
- Print the copy
- Remove Kenny from the copy
- Print the original list
- Print the copy
Exercise 6: Summation
- Using the PromptForInteger and a loop, ask a user to input 10 ints between 1 and 100, store each in a list
- "Please enter an integer (1 to 100): "
- "Your response must be from 1 to 100, try again: "
- print the list of numbers
- print the sum of the numbers (hint: use the sum method)
Exercise 7: Simple Dictionary
Using a dictionary, create a simple contact list with first names and phone numbers.
- Create a function called AddNewContact
- Add a single parameter to the function that will be a dictionary for the user's contacts
- Ask the user for the name of the person (hint: use input() again)
- "Please enter the name of your friend: "
- Use PromptForInteger to get a 7-digit phone number (just to save you typing...), assume that any 7 digit number that starts with 1 or higher is valid
- "What is your friend's 7-digit phone number? "
- "That's not 7 digits, try again: "
- Add the contact info to the dictionary using the name as key and the phone number as value
- In a loop, add 5 contacts to the dictionary
- Import the "pretty printing" library
- Pretty print the dictionary
- Print all of the names in the dictionary (hint: keys)
- Print all of the phone numbers in the dictionary (hint: values)
- Ask the user for the name they want to look up, "Whose number do you want? "
- Print out the number associated with that name
Exercise 8: Nested Dictionaries
While dictionaries are traditionally used in this way - as a collection of unique keys used to "lookup" the same kind of data, it is also possible to use a dictionary to store a collection of named attributes associated with some other data. In this exercise, store a bunch of data with each contact by storing a dictionary in another dictionary!
- Create a new function called AddNewFullContact
- Add a parameter that will be a dictionary that we will store a collection of contacts with their associated contact info
- Ask the user for the following information:
- name - "Please enter the name of your friend: "
- number - "What is your friend's 7-digit phone number? ", "That's not 7 digits, try again: "
- email - "What is your friend's email address? "
- age (between 0 and 125, inclusive) - "What is your friend's age? ", "That's not a real age, try again: "
- Store the "number", "email", and "age" (be sure to use exactly those words) into a new dictionary (not the function's parameter!) that will represent name's contact info
- Store the entire dictionary into the contacts dictionary using name as its key
- Using a loop, add 5 contacts to the dictionary using AddNewFullContact
- Pretty print the dictionary
- Print all of the names in the dictionary (hint: keys)
- Print all of the full contact info in the dictionary (hint: values)
- Ask the user for the name they want to look up, "Whose number do you want? "
- Pretty print this contact's full contact info
- Print out just the email associated with that name
My Code:
################################################################### # Functions ################################################################### def PromptForInteger(min,max): myInteger=int(input("Please enter an integer (1 to 100): ")) while not(myInteger>=min and myInteger<=max): myInteger=int(input("Your response must be from 1 to 100, try again: "))
return myInteger
def promptForInteger(min,max): number=int(input( "Please enter an integer (-50 to 50): ")) while not(number>=min and number<=max): number=int(input( "Your response must be from -50 to 50, try again: "))
temperature=int(input("Please enter your temperature (98 to 106): ")) while not(temperature>=min and temperature<=max): temperature=int(input("You must be dead, try again (98 to 106): " ))
deduction=int(input("Please enter the deduction from your account (-25 to -1): ")) while not(deduction>=min and deduction<=max): deduction=int(input("Your response must be from -25 to -1, try again: "))
menu=int(input("Please enter a menu selection (1 to 10):")) while not(menu>=min and menu<=max): menu=int(input("You must choose 1 to 10, try again: "))
return number,temperature,deduction,menu
################################################################### # 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))
##################################################### # Exercise 2: Custom Range ##################################################### elif exercise == 2: print("Running exercise " + str(exercise)) # Function definition is here # Now you can call changeme function print(promptForInteger(-50,50))
print(promptForInteger(98,106))
print(promptForInteger(-25,-1))
print(promptForInteger(1,10)) ##################################################### # 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) items=items[1:] print(items)
##################################################### # Exercise 5: Display List Elements on Demand and Copying Lists ##################################################### elif exercise == 5: print("Running exercise " + str(exercise))
##################################################### # Exercise 6: Summation ##################################################### elif exercise == 6: print("Running exercise " + str(exercise))# Function definition is here
##################################################### # Exercise 7: Simple Dictionary ##################################################### elif exercise == 7: print("Running exercise " + str(exercise))
##################################################### # Exercise 8: Nested Dictionaries ##################################################### elif exercise == 8: print("Running exercise " + str(exercise))
##################################################### # 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
Get step-by-step solutions from verified subject matter experts
