Как уменьшить количество попыток в цикле? - PullRequest
0 голосов
/ 08 октября 2019

Я создаю игру, в которой вы угадываете точку, и если вы угадываете правильно, вы выигрываете, однако, если вы угадываете неправильно, ваши попытки заканчиваются неудачей. Есть только одна проблема. Попытки не увенчаются успехом. Я не очень хорош в формулировании вопросов, извините.

Я пытался изменить утверждения if, я пытался изменить знак - = просто на знак - какой вид работал, за исключением того факта, что когда-то онзацикливается до исходного числа, которое было до попытки - 1 функция.

while True:
    attempts = 5
    print(attempts)
    x = input("Guess the X coordinate.")
    y = input("Guess the Y coordinate.")

    a = random.randrange(100)
    b = random.randrange(100)

    p = Point(a,b)

    print(p)

    if x and y == a and b:
        print("Yay you did it; you found both coordinates.")
    elif x == a:
        print("You only found the X coordinate.")
    elif x == b:
        print("You only found the Y coordinate")
    else:
        print("you disgust me.")
        print(attempts - 1)

    while attempts < 5:

        if attempts > 1:
            print('you can play again')
            continue
        else:
            print('you lost')
            exit()

1 Ответ

0 голосов
/ 08 октября 2019

Вам нужно использовать attempts += -1:

attempts = 5
while attempts > 0:
    print(attempts)
    x = input("Guess the X coordinate.")
    y = input("Guess the Y coordinate.")
    a = random.randrange(100)
    b = random.randrange(100)

    p = Point(a,b)

    print(p)

    if x and y == a and b:
        print("Yay you did it; you found both coordinates.")
    elif x == a:
        print("You only found the X coordinate.")
    elif x == b:
        print("You only found the Y coordinate")
    else:
        print("you disgust me.")
        attempts += -1
        print(attempts)

    if attempts > 0:
        print('you can play again')
        continue
    else:
        print('you lost')
        exit()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...