Question

In: Computer Science

Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...

Python Exercises = Sentinel Values and While Loops

#Exercise 1
#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 2
#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 3
#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 4
#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 5
#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.

Solutions

Expert Solution

Solution:

Exercise 1 : Following code is used to solve the first exercise. Output has been given along with the code.

#Program depicting sentinel value
sentinel = 8
while sentinel != 1:
    print(sentinel)
    sentinel = int(input())
print("All Done!")

output

8
6
6
5
5
3
3
1
All Done!

Exercise 2: Following while loop will need to be used for the given requirements.

#Exercise 2
sentinel = int(input())
while sentinel !=0:
    print(sentinel)
    sentinel = int(input())
print("Good Bye!")
    

5
5
4
4
3
3
5
5
0
Good Bye!

Exercise 3: Following while loop will need to be used for the given requirements.

#Exercise3
song = None
sentinel = 'y'
while sentinel != 'n':
    song = input("Song?")
    print(song)
    sentinel = input("Continue (y/n)?")
print("Thank you for playing.")

Output

Song?my song is playing
my song is playing
Continue (y/n)?n
Thank you for playing.

Exercise 4: Following while loop will need to be used for the given requirements.

#Exercise4
sentinel = 'n'
while sentinel != 'y':
    g = input("Item?")
    print("Your Grocery item is:" + g)
    sentinel = input("Stop (y/n)?")
print("Thank you for visiting.")

Item?first item
Your Grocery item is:first item
Stop (y/n)?n
Item?second item
Your Grocery item is:second item
Stop (y/n)?y
Thank you for visiting.

Exercise 5 : Following while loop will need to be used for the given requirements.

#Exercise5
x = " "
sentinel = 'c'
while sentinel == 'c':
    item = input("Item?")
    x = x+' '+item
    sentinel = input("c to continue x to exit:")
print("These are your items:"+x)
print("Thank you for shopping.")

output:

Item?first item
c to continue x to exit:c
Item?second item
c to continue x to exit:x
These are your items: first item second item
Thank you for shopping.


Related Solutions

Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
C++ please 1. Write a do while loop that continually loops until the user enters a...
C++ please 1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop. 2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform...
Intro to Python! 1. Using while loops, write a program that prints multiples of 5 which...
Intro to Python! 1. Using while loops, write a program that prints multiples of 5 which are less than 100. Your loop initially starts from 0. 2. Consider B to be a list of alphabetically ordered strings. Using while loops, write a program that only prints words which start with letter A. B = [Alex, Airplane, Alpha, Australia, Ben, Book, Bank, Circuit, Count, Dark] 3. Using while loop, create a program that only prints first 18 multiples of 7.Hint: Your...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user. I am struggling to have my program print the math function. Here is what I have so far: import java.util.*; public class MaxSentinel { public static void main(String[] args) {    Scanner input = new Scanner(System.in); System.out.println("Please enter a value. Press -1 to stop prompt."); int number = 0;...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Write a while loop that will let the user enter a series of integer values and...
Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user backwards. do Loop...
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
Code in python Write a while loop code where it always starts form 2. Then it...
Code in python Write a while loop code where it always starts form 2. Then it randomly chooses a number from 1-4. If the number 4 is hit then it will write “TP” if the number 1 is hit then it will write”SL”. It will rerun the program every time the numbers 1 and 5 are hit. The code should also output every single number that is randomly chosen. 2 of the same numbers can't be chosen back to back...
Important: please use python. Using while loop, write python code to print the times table (from...
Important: please use python. Using while loop, write python code to print the times table (from 0 to 20, incremented by 2) for number 5. Add asterisks (****) so the output looks exactly as shown below.   Please send the code and the output of the program. ****************************************************************** This Program Shows Times Table for Number 5 (from 0 to 20) Incremented by 2 * ****************************************************************** 0 x 5 = 0 2 x 5 = 10 4 x 5 = 20 6...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT