Proper condition loop

68 Views Asked by At

How should I specify my if-else-elif statements to not let them finish checking conditions after the first if-clause?

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

There are 4 best solutions below

0
On

I believe this is what you really want. I removed duplicated code and modified @fozoro code fixing the error in the process

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
On

You could make this easier by changing the order of your conditions so that you only get to asking for another number once all exit conditions are dealt with (i.e. winning or losing):

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
On

Fully working code:

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
2
On

You should replace the elif statements with if statements like this:

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