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

  • SEARCH
  • Home
  • 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 8558299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:46:48+00:00 2026-06-11T15:46:48+00:00

I was just wondering if I could remove some redundancy for a help function

  • 0

I was just wondering if I could remove some redundancy for a help function in a text-game. What I have right now is at the start of every function hint = 0, and hint is increased one every time an invalid answer is entered.

Here is what I have at the moment (inside every function):

hint = 0
    valid = False
    while valid == False:

        print "Would you like to begin?"

        begin = raw_input("> ")

        if "yes" in begin:
            valid = True
            print "Great!\n"
            start.start()
        elif "no" in begin:
            quit.quit()
        else:
            error.error(1)
            hint += 1

        if hint > 4:
            print "\nYou may choose from \"yes\" and \"no\"."
  • 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-06-11T15:46:49+00:00Added an answer on June 11, 2026 at 3:46 pm

    The following code use decorator to separate hint logic and command logic. Every command is handled by a function which can be decorated by Hint object.

    When the function return False, the count in Hint object will increase and when it’s larger than the limit, it will print out a hint message.

    When the function return a tuple it will be called by the main loop.

    class Hint(object):
        def __init__(self, n, msg):
            self.n = n
            self.msg = msg
    
        def __call__(self, f):
            def wrap(*args, **kw):
                count = 1
                while True:
                    ret = f(*args, **kw)
                    if ret == False:
                        count += 1
                        if count > self.n:
                            print self.msg
                            count = 0
                    else:
                        break
                return ret
            return wrap
    
    def start_at(place):
        print "start at %d" % place
        return "start"
    
    @Hint(3, "You may choose from 1, 2, 3.")
    def start():
        print "What place will you start?"
        cmd = raw_input("> ")
        try:
            place = int(cmd)
            if place not in (1,2,3):
                return False
            else:
                return start_at, (place,)
        except ValueError:
            return False
    
    def quit():
        print "I will quit"
        return "quit"
    
    @Hint(4, "You may choose from yes and no.")
    def begin():
        print "Would you like to begin?"
        cmd = raw_input("> ")
        if "yes" in cmd:
            print "Great!\n"
            return start, ()
        elif "no" in cmd:
            print "Bad!\n"
            return quit, ()
        else:
            return False
    
    call_func, args = begin, ()
    while True:        
        ret = call_func(*args)
        if isinstance(ret, tuple):
            call_func, args = ret
        else:
            break
    

    Here is some test:

    Would you like to begin?
    > abc
    Would you like to begin?
    > def
    Would you like to begin?
    > 123
    Would you like to begin?
    > 345
    You may choose from yes and no.
    Would you like to begin?
    > yes
    Great!
    
    What place will you start?
    > 5
    What place will you start?
    > fg
    What place will you start?
    > sd
    You may choose from 1, 2, 3.
    What place will you start?
    > 2
    start at 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just wondering how I could remove everything after a certain substring in PHP
Just wondering if you could help wanting to produce an activity stream in Java,
Just wondering if someone could help me with a very simple SQL query. I
Just wondering if someone could point me in the right direction of how to
I'm just wondering if someone could point me in the right direction here, I
Hi sorry still learning here and slow to learning code arguments. Just wondering could
I just wondering how I could replace the second instance of a string inside
I'm just wondering how I could display my last 200 mysql entries in php
I’m just wondering if anyone’s come across or could recommend an IDE that’s capable
Hey, I'm just wondering what code I could use to auto change the size

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.