python猜数字 - 在重复之后再试一次

时间:2015-05-28 23:57:30

标签: python random numbers

我做了一个简单的猜数字程序,但我不知道如何在猜错3次之后再试一次。任何提示和技巧?

from random import randint
for i in range(1): 
random_value = randint (1,10)

print "Guess the number"
tryes = 1
while tryes <= 3:
guess_1 = input('>')

    if guess_1 > random_value:
        print "The number is lower"
        tryes = tryes + 1
    elif guess_1 < random_value:
        print "The number is higher"
        tryes = tryes + 1
    elif guess_1 == random_value:
        print "You guessed right"
        tryes = tryes + 3
    else:
        print "Invalid answer"

print "You won! Lets try that again shall we?"

1 个答案:

答案 0 :(得分:0)

from random import randint
while True: #this will just make it loop forever ...
    #for i in range(1):  ## No idea what this was meant to do ...
    random_value = randint (1,10)

    print "Guess the number"
    tryes = 1
    while tryes <= 3:
        guess_1 = input('>')

        if guess_1 > random_value:
            print "The number is lower"
            tryes = tryes + 1
        elif guess_1 < random_value:
            print "The number is higher"
            tryes = tryes + 1
        elif guess_1 == random_value:
            print "You guessed right"
            tryes = tryes + 3
        else:
            print "Invalid answer"

    print "You won! Lets try that again shall we?"