如何改进-数字生成器+范围内的猜测数字

时间:2019-01-07 10:21:36

标签: python

我试图创建从X到Y的数字范围-检查输入是否为数字。下一个猜测级别(仅3次尝试):验证“猜测”是否为数字+是否来自范围。如何简化+我陷入了循环。

 from random import randint

# Generates a number from x through y inclusive

while True:
    x1 = raw_input("Please provide x - first digit from the range:  ")
    try: 
        x = int(x1)
        while True:
            y1 = raw_input("Please provide y - last digit from the range:  ")
            try: 
                y = int(y1)
                random_number = randint(x, y)
                guesses_left = 3
                while guesses_left > 0:
                    guess = raw_input("Your guess: ")
                    guesses_left -= 1
                    try:
                        val = int(guess)
                        if val < x:
                            guesses_left += 1
                            print "Another number from range from", x, "to " ,  y
                        if val > y:
                            guesses_left += 1
                            print "Another number from range from", x, "to " ,  y
                        elif val == random_number:
                            print "You win!"
                            break
                    except ValueError:
                        print "That's not digit!"
                        guesses_left += 1
                else:
                    print "You lose."  
                    break  
            except ValueError:
                print "That's not digit!"
    except ValueError:
        print "That's not digit!"

3 个答案:

答案 0 :(得分:1)

在顶层,您的代码具有以下内容:

while True:
    x1 = raw_input("Please provide x - first digit from the range:  ")
    try: 
        x = int(x1)
        while True:
            y1 = raw_input("Please provide y - last digit from the range:  ")
            try: 
                y = int(y1)
                random_number = randint(1, 10)
            # give three guesses

            except ValueError:
                print "That's not digit!"
    except ValueError:
        print "That's not digit!"     

无法逃避while循环。

也许在猜测之后停下来?

while True:
    x1 = raw_input("Please provide x - first digit from the range:  ")
    y1 = raw_input("Please provide y - last digit from the range:  ")
    try: 
        x = int(x1)
        y = int(y1)
        random_number = randint(x, y+1) #<-- note change to number picked
        # give three guesses

        break #<-- way to escape the loop
    except ValueError:
        print "That's not digit!"     

答案 1 :(得分:0)

我建议首先按照马丁·福勒(Martin Fowler)的《重构改进现有代码的设计》一书中所述的“提取方法/功能”重构方法。

这里是重构功能/方法的简要介绍: https://refactoring.com/catalog/extractFunction.html

接下来可能有用的步骤是: 提取(描述性)变量 https://refactoring.com/catalog/extractVariable.html

然后,您可能会考虑从此代码创建类,以很好的方式封装它。

如果您想了解规则并多次使用它,可以考虑阅读这本书: https://martinfowler.com/books/refactoring.html

答案 2 :(得分:0)

我对您的程序进行了一些重组。仍有很多改进的可能。我删除了很多嵌套。您有多个嵌套的while循环,其中try除外。它使您难以阅读,也很难在代码中发现错误。

from random import randint

# Generates a number from x through y inclusive

def startGame():
    while True:
        try:
            x1 = raw_input("Please provide x - first digit from the range:  ")
            x = int(x1)
            y1 = raw_input("Please provide y - last digit from the range:  ")
            y = int(y1)
            if x > y:
                x,y=y,x
            break
        except ValueError:
            print "That's not digit!"

    random_number = randint(x, y)
    guesses_left = 3
    while guesses_left > 0:
        guess = raw_input("Your guess: ")
        try:
            val = int(guess)
            if x > val or val > y :
                print ("Another number from range from {} to {}".format(x,y))
            elif val == random_number:
                print "You win!"
                break
            else:
                guesses_left -= 1
                print("You have {} guesses left".format(guesses_left))
        except ValueError as e:
            print "That's not digit!"

    else:
        print "You lose."   

game_loop = True
while game_loop:
    startGame()
    user_inp = raw_input("Do you want to play again y/n ?\n")
    if user_inp not in "yYyesYes":
        game_loop = False