简体   繁体   中英

Proper condition loop

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

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 (ie 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:")

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

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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM