0

I have to check if the input year is a a valid 4 digit no. Which isn't a float value or has more than 4 characters. The input is from a file dictionary type.

If not I want to exit the program and don't want to continue with the steps and functions after.

But the only challenge is I'm trying to do it without the use of libraries like sys etc. Is this possible in Python?

3
  • 1
    exit() is builtin. You can just use it. Commented May 1, 2020 at 1:51
  • @tdelaney doesn't work for me. The program is not terminated Commented May 1, 2020 at 1:54
  • Sure it gets called? Try placing a print statement just before exit, to verify it gets evaluated. Commented May 1, 2020 at 2:17

2 Answers 2

1

try to use:

exit("some message")

it is a built in function

Sign up to request clarification or add additional context in comments.

Comments

0

You can check if the input is 4 digits or not by using input<10000 and input>999. You can check if the input is a float or not by using the type keyword. So, your program that you described would look something like this:

if x>999 and x<10000:
  if type(x) != float:
    print("Valid")
  else:
    print("Invalid")
else:
  print("Invalid")

Hope this helps!

1 Comment

Need to exit from the program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.