In: Computer Science
Step by step in python
Write a program that will keep asking for a user input (until a blank line is entered) and will inform me whether what I entered was a valid number or not (without crashing).
Answer:
here is the python code as per your requirement
Raw code;
#function named check validity which takes no arguments
def checkValidity():
#infinite loop
while(1):
#get input from user
val=input("Enter value to check valid or not: ")
#if user entered newline
if val=='':
#break the loop and terminate
break
#else
else:
#try
try:
#convert the value to float
val=float(val)
#if it can be converted then its valid number
print("valid number")
#exception
except:
#print it as non valid
print("not valid number")
#continue to iterate the loop
continue
#main method
def main():
#call the above function in main
checkValidity()
#calling main
main()
Editor:
output:
Hope this helps you! If you still have any doubts or queries please feel free to comment in the comment section.
"Please refer to the screenshot of the code to understand the indentation of the code".
Thank you! Do upvote.