Question: Python #Tip: Use only one variable name for each exercise #Exercise 1 #Write a while loop with the following structure: #Use an input statement (no
Python
#Tip: Use only one variable name for each exercise
#Exercise 1 #Write a while loop with the following structure: #Use an input statement (no prompt) to ask the user for a number and assign this number to a variable #Convert the user input to an integer and assign to the same variable #Loop Body: #Condition: while value of variable is greater than zero #Action: display the number assigned to the variable #Increment: subtract 1 from the variable (assign the result to the same variable -- e.g. x = x - 1 if the variable is x) #After the while loop ends, display the following: While loop is over!
#Exercise 2 #Write a while loop with the following structure: #Use an input statement (no prompt) to ask the user for a number and assign this number to a variable #Convert the variable to an integer and assign the converted number to the same variable #Loop Body #Condition: while variable is less than zero #Action: display the number assigned to the variable #Increment: add 1 to the variable and assign the result to the same variable #After the while loop ends, display the following: All done!
#Exercise 3 #Write a while loop with the following structure: #Use an input statement (no prompt) to ask the user for a number and assign this number to a variable #Convert the variable to an integer and assign the converted number the same variable #Loop Body: #Condition: while variable is less than zero #Action: display the number assigned to the variable #Increment: add 1 to the variable and assign the result to the same variable #For every loop iteration, display the following: One more loop! #After the while loop ends, display the following: All done!
#Exercise 4 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the sentinel value variable #Convert the input to an integer and assign the converted number to the same variable #After the while loop ends, display the following statement on screen: All done!
#Exercise 5 #Write a while loop with a sentinel value #This while loop ends when the user enters a zero, the sentinel value #Use an input statement to ask the user for an initial value (no prompt) and assign it to a sentinel value variable #Convert the variable to an integer and assign the converted number to the same variable #Condition: while the variable is not equal to 0 #Action: display the number assigned to the variable #Ask the user for a number by using an input statement (no prompt) and assign this number to the sentinel value variable #Convert the variable to an integer and assign the converted number to the same variable as well #After the while loop ends, display the following statement on screen: Goodbye!
#Exercise 6 #Note: in this exercise you need to keep track of 2 variables, one assigned the sentinel and another assigned a song #This time you will use a string as the sentinel value. This while loop ends when the user enters an n #Initialize your sentinel value by assigning the string y to a variable that will store the sentinel value #Condition: while the sentinel variable is not equal to 'n' #Ask the user for a song title by using an input statement with the prompt: Song? and assign the input to a new variable #Display the user entered song on screen #Ask the user for the sentinel value with an input statement with the prompt: Continue (y/n)? #and assign value to the variable assigned the sentinel #After the while loop ends, display the following on screen: Thank you for playing
#Exercise 7 #This time you will again use a string as the sentinel value. This while loop ends when the user enters a y #Note: in this exercise you need to keep track of 2 variables, one assigned the sentinel and another assigned a grocery item #Initialize your sentinel value by assigning the string n to a variable to store the sentinel #Condition: while sentinel variable is equal to 'n' #Ask the user for a grocery item by using an input statement with the prompt: item? and assign the input to a new variable #Display the user entered item (variable name g) on screen in the following format: #Your grocery item is: [grocery item] (Tip, you'll need a concatenator) #Ask the user for the sentinel value with an input statement with the prompt: Stop (y/n)? #and assign value to variable assigned the sentinel #After the while loop ends, display the following on screen: Thank you for visiting
#Exercise 8 -- Extra Credit #This uses a string as the sentinel value and collects user entries to be printed out at the end of the while loop #1. Initialize your sentinel value by assigning the string c to a variable (this is your sentinel) #2. Initialize another variable to collect user entries as an empty string, for example x = " " [space between the quotes] #3. Condition: while sentinel is equal to 'c' #4. Ask the user for an item name by using an input statement with the prompt: item? and assign the input to a new variable #5. Concatenate the item to the variable containing the space (initalized in instruction 2, e.g. x = x+' '+item) #6. Ask the user for the sentinel value with an input statement (assign to sentinel) with the prompt: #7 c to continue x to exit: (no space after colon) #8. After the while loop ends, print the following statement, substituting the variable updated w instruction 5 #9. (use the + or , concatenator): #10. These are your items: [variable updated w instruction 5] #11. Display the following as the last line: Thank you for shopping.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
