循环故障时(仅处理所有其他输入)

时间:2016-10-27 19:09:32

标签: python while-loop

这是一个猜谜游戏程序,用户会想到一个数字,程序会试图猜测它。它不会响应每个输入,而是忽略所有其他输入。对不起,如果我的代码太多了!

#Instructions for user
def instructions():
    print("Please think of a number between 1 and 1000, inclusive.")
    input("Press enter when ready to continue.")
    print("""
Enter -1 if your number is smaller than my guess
       1 if your number is bigger than my guess
       0 if I guess your number
""")
    print()

# Prompts user to continue
def prompter():
    input("Press enter when ready to continue.")
    print()

# Guesses number
def guesser(upper, lower):
    import math

    guessVal = math.ceil((upper + lower - 1)/2)
    feedback = input("Is your number " + str(guessVal) + "? ")
    return [feedback, guessVal]

# Evaluates data and gives appropriate response
def processData(feedback, tries, upper, lower, guessVal):
    if feedback == "1":
        lower = guessVal + 1
    elif feedback == "-1":
        upper = guessVal - 1
    elif feedback == "0":
        print("I guessed your number in " + str(tries) + " tries!")
        tryAgainOp()
    else:
        print("That's not what I asked for!")
        tries -= 1
        guesser(upper, lower)
    return [lower, upper]

# Bound checker
def boundChecker(upper, lower):
    if upper > 1000:
        errorBound = "yes"
    elif lower < 1:
        errorBound = "yes"
    else:
        errorBound = "no"
    return errorBound

# Trick-me checker
def trickChecker(upper, lower):
    if upper < lower:
        errorTrick = "yes"
    else:
        errorTrick = "no"
    return errorTrick

# Are you trying to trick me?
def trickingMe():
    print("Are you trying to trick me? Try again!")

# Out of bounds
def invalidBounds():
    print("Out of bounds! Try again!")

# Try again option
def tryAgainOp():
    opAns = input("Try again? Enter \"no\" to exit and anything else to continue.")
    return opAns

# Display an exit message
def exitMessage():
    print("Thank you for using this program! Have a nice day.")

# Sets up part of main module
def partialMain(tries, upper, lower):
    while tries <= 15:
        feedbackGuessVal = guesser(upper, lower)
        feedback = feedbackGuessVal[0]
        guessVal = feedbackGuessVal[1]
        bounds = processData(feedback, tries, upper, lower, guessVal)
        lower = bounds[0]
        upper = bounds[1]
        errorBound = boundChecker(upper, lower)
        errorTrick = trickChecker(upper, lower)

        if errorBound == "yes":
            return
        elif errorTrick == "yes":
            return

        tries += 1

    return [errorBound, errorTrick, tries]

# Main module
def main():
    # Initializes constants
    upper = int(1000)
    lower = int(1)
    tries = int(0)
    errorBound = ""
    errorTrick = ""
    opAns = ""

    instructions()
    prompter()

    while opAns.lower() != "no":
        boundTrickTries = partialMain(tries, upper, lower)
        errorBound = boundTrickTries[0]
        errorTrick = boundTrickTries[1]
        tries = int(boundTrickTries[2])
        print()
        if errorBound == "yes":
            invalidBounds()
            errorBound = ""
        if errorTrick == "yes":
            trickingMe()
            errorTrick = ""
        if tries > 15:
            print("Too many tries! Try again!")
            tries = 0

    opAns = tryAgainOp()

    exitMessage()
    prompter()

main()

0 个答案:

没有答案