In: Computer Science
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.
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.