Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3758770
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:12:48+00:00 2026-05-19T10:12:48+00:00

I have the following snippet: def check1(n): if len(n) != 4: return raw_input(Enter 4

  • 0

I have the following snippet:

def check1(n):
    if len(n) != 4:
        return raw_input("Enter 4 digits only")
    else:
        return True

def check2(n):
    if n.isdigit() != True:
        return raw_input("Enter digits only")
    else:
        return True

def check3(n):
    if len(set(str(n))) != 4:
        return raw_input("Enter non duplicate numbers only")
    else:
        return True

sturn = 1
lturn = 8       

a = raw_input("Enter the 4 numbers you want to play with: ")

for turn in range(sturn, lturn):
    b = raw_input("Enter your guess: ")
    if (check1(b) != True or check2(b) != True or check3(b) != True):
        if check1(b) != True:
            print check1(b)
        elif check2(b) != True:
            print check2(b)
        elif check3(b) != True:
            print check3(b)
    else:
        print b

How can I rewrite this such that if any of the check functions fail, it starts from the b = raw_input line again and retests all the checks.

UPDATE
I’ve improved the code after heeding the advice from keithjgrant and m1k3y02 but it doesn’t work properly. If I enter ‘1’ consecutively, it bounces between different exceptions instead of staying on the first check.

def checks(n):
    if len(n) != 4 or n.isdigit() != True or len(set(str(n))) != 4:
        return False
    else:
        return True

sturn = 1
lturn = 8       

a = raw_input("Enter the 4 numbers you want to play with: ")

for turn in range(sturn, lturn):
    b = raw_input("Enter your guess: ")
    while checks(b) != True:
        if len(b) != 4:
            b = raw_input("Enter 4 digits only")
        if b.isdigit() != True:
            b = raw_input("Enter digits only")
        if len(set(str(b))) != 4:
            b = raw_input("Enter non duplicate numbers only") 

    print b
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:12:48+00:00Added an answer on May 19, 2026 at 10:12 am

    Your question, as it stands, has nothing to do with exceptions. Try the following:

    checks = [
        lambda n: (len(n)==4, "Enter 4 digits only."),
        lambda n: (n.isdigit(), "Enter digits only."),
        lambda n: (len(set(str(n)))==4, "Enter non duplicate numbers only.")
    ]
    
    a = raw_input("Enter the 4 numbers you want to play with: ")
    
    sturn = 1
    lturn = 8
    for turn in range(sturn, lturn):
        b = raw_input("Enter your guess: ")
        all_good = True
    
        for check in checks:
            good,msg = check(b)
            if not good:
                print msg
                all_good = False
                break
    
        if all_good:
            print "{0} is correct!".format(b)
            break
    

    You could rewrite it to use exceptions as follows:

    class CheckError(Exception): pass
    
    class WrongLengthError(CheckError): pass
    def check1(n):
        if len(n) != 4:
            raise WrongLengthError("Enter 4 digits only.")
    
    class NonDigitCharError(CheckError): pass
    def check2(n):
        if not n.isdigit():
            raise NonDigitCharError("Enter digits only.")
    
    class HasDuplicatesError(CheckError): pass
    def check3(n):
        if len(set(str(n))) != 4:
            raise HasDuplicatesError("Enter non duplicate numbers only.")
    
    
    a = raw_input("Enter the 4 numbers you want to play with: ")
    
    sturn = 1
    lturn = 8
    for turn in range(sturn, lturn):
        b = raw_input("Enter your guess: ")
    
        try:
            for check in (check1, check2, check3):
                check(b)
    
            print "{0} is correct!".format(b)
            break
        except CheckError, c:
            print c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have the following Code Snippet; class StringCalci { static def plus(Integer self,
I have following code snippet and i am trying to evaluate the time take
I have following code snippet that i use to compile class at the run
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have the following snippet in one of my html pages : <div class=inputboximage>
I have the following snippet of code, changeTextArea is a TextArea object. changeTextArea.addKeyboardListener(new KeyboardListenerAdapter()
I have the following snippet where I would like to extract code between the
I have the following snippet of code that's generating the Use new keyword if
We have the following snippet. OSStatus createErr = PasteboardCreate(kPasteboardClipboard, &m_pboard); if (createErr != noErr)
I have the following snippet from my code: switch ($extention) { case gif: $src

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.