Program 2: “Multiplication Facts” The intent of this program is to allow a user to enter an integer and then be presented with the first 10 multiples (multiplication facts) of that integer. The user must be notified of a sentinel value. The user will continue to be prompted to enter an integer and see the multiples until the sentinel value is entered at which point the program will stop. Use a while loop to get the inputs and a for loop to print out the facts:
Here's a Python program that implements the requirements you've outlined for the "Multiplication Facts" program. The program will prompt the user to enter an integer and display the first 10 multiples of that integer. The user can continue entering integers until they enter a sentinel value to exit the program.
# Program Header
print("########## Program #2 – Multiplication Facts ###########")
# Sentinel value
sentinel = -1 # You can choose any value that makes sense as a sentinel
while True:
# Prompt user for input
user_input = input(f"Enter an integer to see its multiplication facts (or {sentinel} to exit): ")
# Convert input to integer
try:
number = int(user_input)
except ValueError:
print("Please enter a valid integer.")
continue
# Check for sentinel value
if number == sentinel:
print("Exiting the program.")
break
# Print multiplication facts using a for loop
print(f"Multiplication facts for {number}:")
for i in range(1, 11):
result = number * i
print(f"{number} x {i} = {result}")
-1
is defined. This value will be used to exit the loop.while True
loop, which will continue until the user enters the sentinel value.try-except
block to handle invalid inputs (non-integer values).for
loop runs from 1 to 10, calculating and printing the multiplication facts for the entered integer.-1
(or the chosen sentinel value) to exit