In Python, which of code listed below validates a postive float? A.) strMyNumber
ID: 3856825 • Letter: I
Question
In Python, which of code listed below validates a postive float?
A.)
strMyNumber = input("Enter a positive float: ")
while flag == False:
try:
fltMyNumber = float(strMyNumber)
except ValueError:
strMyNumber = input("Sorry you did not enter a valid number. Please try again: ")
#end try block
#end loop
B.)
strMyNumber = input("Enter a positive float: ")
while flag == True:
try:
fltMyNumber = float(strMyNumber)
except ValueError:
strMyNumber = input("Sorry you did not enter a valid number. Please try again: ")
#end try block
#end loop
C.)
while flag == True:
if strMyNumber.isdigit():
fltMyNumber = float(strMyNumber)
flag = True
#end loop
D.
strMyNumber = input("Enter a positive float: ")
while flag == False:
try:
fltMyNumber = float(strMyNumber)
if fltMyNumber >= 0:
flag = True
else:
#ask user for another value
strMyNumber = input("Sorry you did not enter a valid number. Please try again: ")
#end if
except ValueError:
strMyNumber = input("Sorry you did not enter a valid number. Please try again: ")
#end try block
#end loop
A.)
strMyNumber = input("Enter a positive float: ")
while flag == False:
try:
fltMyNumber = float(strMyNumber)
except ValueError:
strMyNumber = input("Sorry you did not enter a valid number. Please try again: ")
#end try block
#end loop
Explanation / Answer
Answer:D
D.
strMyNumber = input("Enter a positive float: ")
while flag == False:
try:
fltMyNumber = float(strMyNumber)
if fltMyNumber >= 0:
flag = True
else:
#ask user for another value
strMyNumber = input("Sorry you did not enter a valid number. Please try again: ")
#end if
except ValueError:
strMyNumber = input("Sorry you did not enter a valid number. Please try again: ")
#end try block
#end loop
Explanation:
1)in python code if we first check that the given float value is >(greater than) or = to zero
2)if value >= 0 then
3)we received the value please check it it can be >(greater than) zero.
4)if so throw another an exception,after that
5)we can be place a while loop so program can ve run continues repeating until valid answer is received from the end user, and tests for the right input inside it
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.