In Python, which segment of code listed below is used to validate a positive int
ID: 3856828 • Letter: I
Question
In Python, which segment of code listed below is used to validate a positive integer?
A.)
while flag == True:
if strMyNumber.isdigit():
fltMyNumber = float(strMyNumber)
flag = True
#end loop
B.)
while flag == False:
if strMyNumber.isdigit():
fltMyNumber = float(strMyNumber)
#end loop
C.)
while flag == True:
if strMyNumber.isdigit():
intMyNumber = int(strMyNumber)
#end loop
D.)
while flag == False:
if strMyNumber.isdigit():
intMyNumber = int(strMyNumber)
flag = True
#end loop
Explanation / Answer
Option D is the answer
initially flag = false. The loop runs as long as the flag remains false
isdigit function of the string object returns True if the value represented by the string strMyNumber is a valid positive integer. When a valid positive integer is found, making flag = true will break the loop. At the end intMyNumber will have positive integer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.