Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote