-1

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
smci
  • 32,567
  • 20
  • 113
  • 146
MaciejGy
  • 31
  • 2
  • 2
    Welcome to StackOverflow. I have a slightly off-topic question: where does this exercise come from? What book, tutorial, article, or class is assigning it? I've seen it on StackOverflow many times and ... let's just say I have some opinions. – FMc Mar 21 '19 at 23:10
  • 1
    hey, the similar exercise was shown on freecodecamp video of python on YouTube – MaciejGy Mar 21 '19 at 23:14
  • Your question is about writing an if-else-elif statement ladder and not having **fall-through** between multiple clauses that can be hit with the same value. Note that `answer != x`, `answer > x`, `answer < x` overlap; how do you expect those cases to be handled? Which clauses do you want to hit? – smci Mar 22 '19 at 00:52

4 Answers4

1

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:")
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

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
Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
  • Now I'm getting syntax error about my last if, it states that there cannot be > between string and int, didn't have it before – MaciejGy Mar 21 '19 at 23:11
  • Oh right, didn't notice it at first. This is due to the fact that you are trying to compare and integer (in your case `x`) with a string (in your case `answer`).This can be easily fixed by converting `answer` to an integer using `int()`. (I've just edited my answer, it should work now.) – Nazim Kerimbekov Mar 21 '19 at 23:16
0

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")
tushortz
  • 3,697
  • 2
  • 18
  • 31
0

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
MaciejGy
  • 31
  • 2