Sentinel Value- Python Coding This program runs to figure out if numbers the user enters are odd or even. The While loop within the try block is supposed to repeat until the user types in "1". However, the while loop does not continue to run, it just exits after 1 run through. Below is my coding so far: # Description:Give instructions for the game and a tell them to input a 3 digital number ONLY, use a while loop. determine if numbers are odd or even.#give instructions using print statementsprint("In this Odd and Even game, you will enter a three digit number (eg.145) and we will tell you if it is odd or even!")print("Type '1' to quit")while True:#try blocktry:#enter a three-digit numbernum=int(input('Enter a three-digit number only (type "1" to quit): '))#check if number of digits in number is 3 or notoddList=[]evenList=[]while num !=1:if len(str(num))==3:#check if number is evenif num%2==0:print("Your number,%s, is even"%(num))evenList.append(int(num))#if number is not even then print oddelse:print("Your number,%s, is odd"%(num))oddList.append(int(num))#first number odd or even#make number a listlistNum=list(str(num))firstNum=listNum[0]#if and else statements for odd or evenif int(firstNum)%2==0:print("%s is even."%(firstNum))evenList.append(int(firstNum))else:print("%s is odd"%(firstNum))oddList.append(int(firstNum))secondNum=listNum[1]if int(secondNum)%2==0:print("%s is even"%(secondNum))evenList.append(int(secondNum))else:print("%s is odd"%(secondNum))oddList.append(int(secondNum))thirdNum=listNum[2]if int(thirdNum)%2==0:print("%s is even"%(thirdNum))evenList.append(int(thirdNum))else:print("%s is odd"%(thirdNum))oddList.append(int(thirdNum))#sum of all digitssum=0#find sum#make string numbers integersfirstNum=int(firstNum)secondNum=int(secondNum)thirdNum=int(thirdNum)sum=firstNum+secondNum+thirdNumprint("The sum of all individual numbers: %s+%s+%s=%s"%(firstNum,secondNum,thirdNum,sum))if sum%2==0:print("%s is an even number"%(sum))evenList.append(int(sum))else:print("%s is an odd number"%(sum))oddList.append(int(sum))print("odd numbers:%s"%(oddList))print("even numbers:%s"%(evenList))#if length of number is not 3 then raise value error exceptionelse:raise ValueErrorbreakprint("your odd numbers: %s"%(oddList))print("your even numbers: %s"%(evenList))exit()#except blockexcept ValueError:#print statement if number is not 3 digitsprint('Not a three-digit number')
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
Sentinel Value- Python Coding
This program runs to figure out if numbers the user enters are odd or even.
The While loop within the try block is supposed to repeat until the user types in "1". However, the while loop does not continue to run, it just exits after 1 run through.
Below is my coding so far:
# Description:Give instructions for the game and a tell them to input a 3 digital number ONLY, use a while loop. determine if numbers are odd or even.
#give instructions using print statements
print("In this Odd and Even game, you will enter a three digit number (eg.145) and we will tell you if it is odd or even!")
print("Type '1' to quit")
while True:
#try block
try:
#enter a three-digit number
num=int(input('Enter a three-digit number only (type "1" to quit): '))
#check if number of digits in number is 3 or not
oddList=[]
evenList=[]
while num !=1:
if len(str(num))==3:
#check if number is even
if num%2==0:
print("Your number,%s, is even"%(num))
evenList.append(int(num))
#if number is not even then print odd
else:
print("Your number,%s, is odd"%(num))
oddList.append(int(num))
#first number odd or even
#make number a list
listNum=list(str(num))
firstNum=listNum[0]
#if and else statements for odd or even
if int(firstNum)%2==0:
print("%s is even."%(firstNum))
evenList.append(int(firstNum))
else:
print("%s is odd"%(firstNum))
oddList.append(int(firstNum))
secondNum=listNum[1]
if int(secondNum)%2==0:
print("%s is even"%(secondNum))
evenList.append(int(secondNum))
else:
print("%s is odd"%(secondNum))
oddList.append(int(secondNum))
thirdNum=listNum[2]
if int(thirdNum)%2==0:
print("%s is even"%(thirdNum))
evenList.append(int(thirdNum))
else:
print("%s is odd"%(thirdNum))
oddList.append(int(thirdNum))
#sum of all digits
sum=0
#find sum
#make string numbers integers
firstNum=int(firstNum)
secondNum=int(secondNum)
thirdNum=int(thirdNum)
sum=firstNum+secondNum+thirdNum
print("The sum of all individual numbers: %s+%s+%s=%s"%(firstNum,secondNum,thirdNum,sum))
if sum%2==0:
print("%s is an even number"%(sum))
evenList.append(int(sum))
else:
print("%s is an odd number"%(sum))
oddList.append(int(sum))
print("odd numbers:%s"%(oddList))
print("even numbers:%s"%(evenList))
#if length of number is not 3 then raise value error exception
else:
raise ValueError
break
print("your odd numbers: %s"%(oddList))
print("your even numbers: %s"%(evenList))
exit()
#except block
except ValueError:
#print statement if number is not 3 digits
print('Not a three-digit number')
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images