Правильное условие цикла - PullRequest
0 голосов
/ 22 марта 2019

Как мне указать мои операторы if-else-elif, чтобы не позволить им завершить проверку условий после первого предложения if?

import random
x = random.randint(1, 100)
correct_answer = False
guess_count = 0
answer = input("Try to guess a number in range of 1 to 100... ")
while guess_count < 6 and correct_answer == False:
    if answer != x:
        answer = input("Try again...")
        guess_count = guess_count + 1
    elif answer > x:
        print("Try lower number")
        guess_count = guess_count + 1
    elif answer < x:
        print("Try higher number")
        guess_count = guess_count + 1
    elif answer == x:
        print("You won!")
        correct_answer = True
    elif guess_count > 6:
        print("You ran out of chances, sorry")
        break

Ответы [ 4 ]

1 голос
/ 22 марта 2019

Вы могли бы упростить это, изменив порядок своих условий так, чтобы вы могли запрашивать другой номер только после того, как будут выполнены все условия выхода (т.е. выигрыш или проигрыш):

import random
x = random.randint(1, 100)
correct_answer = False
guess_count = 0
answer = input("Try to guess a number in range of 1 to 100... ")
while True:
    guess_count = guess_count + 1
    answer = int(answer)
    if answer == x:
        print("You won!")
        correct_answer == True
        break
    if guess_count > 6:
        print("You ran out of chances, sorry")
        break
    if answer > x:
        answer = input("Try a lower number:")
    else:
        answer = input("Try a higher number:")
0 голосов
/ 22 марта 2019

Полностью рабочий код:

import random
x = random.randint(1, 100)
correct_answer = False
guess_count = 0
answer = int(input("Try to guess a number in range of 1 to 100... "))
while guess_count < 6 and correct_answer == False:
    if answer != x and answer > x:
        answer = int(input("Try again... The number should be lower "))
        guess_count = guess_count + 1
    if answer != x and answer < x:
        answer = int(input("Try again... The number should be higher "))
        guess_count = guess_count + 1
    if answer == x:
        print("You won!")
        correct_answer = True
    if guess_count > 5:
        print("You ran out of chances, sorry")
        break
0 голосов
/ 22 марта 2019

Я считаю, что это то, что вы действительно хотите. Я удалил дублированный код и изменил код @fozoro, исправляя ошибку в процессе

import random

x = random.randint(1, 100)
correct_answer = False
answer = int(input("Try to guess a number in range of 1 to 100...: "))
guess_count = 1

while guess_count < 6 and correct_answer == False:
    guess_count = guess_count + 1

    if answer != x:
        answer = int(input("Try again...: "))

    if answer > x:
        print("Try lower number")

    if answer < x:
        print("Try higher number")

    if answer == x:
        print("You won!")
        correct_answer = True

    if guess_count >= 6:
        print("You ran out of chances, sorry")
0 голосов
/ 22 марта 2019

Вы должны заменить операторы elif на операторы if, например:

import random
x = random.randint(1, 100)
correct_answer = False
guess_count = 0
answer = int(input("Try to guess a number in range of 1 to 100... "))
while guess_count < 6 and correct_answer == False:
    if answer != x:
        answer = int(input("Try again..."))
        guess_count = guess_count + 1
    if answer > x:
        print("Try lower number")
        guess_count = guess_count + 1
    if answer < x:
        print("Try higher number")
        guess_count = guess_count + 1
    if answer == x:
        print("You won!")
        correct_answer = True
    if guess_count > 6:
        print("You ran out of chances, sorry")
        break
...