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
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)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
