简体   繁体   中英

Nested Loops for a Higher/Lower Game

import random

seedVal = int(input("What seed should be used? "))
random.seed(seedVal)

while (True):
    lower = int(input("What is the lower bound? "))
    upper = int(input("What is the upper bound? "))
    number = random.randint(min(lower, upper), max(lower, upper))

    if (lower >= upper):
        print("Lower bound must be less than upper bound.")

    else:
        guess = int(input("What is your guess? "))
        if (guess < number):
            print("Nope, too low.")

        elif (guess > number):
            print("Nope, too high.")

        elif (guess == number):
            print("You got it!")
            break

        else:
            print("Guess should be between lower bound and upper bound.")

This is my code so far for a higher/lower game I'm working on for my scripting class. The issue I run into when I test it is: the nested if/else statement goes back to the beginning of the while loop after an incorrect guess, such as "Nope, too low". I understand this is how a while loop works; However, I cannot figure out how to get the if/else statement to continue on without prompting the user for another lower/upper bound. My best guess is, obviously, to use a nested loop I just don't know how to in this situation.

you don't need a nested loop but just to get the set up of upper/lower in a different loop.

By the way, you will never get to the else at the end since you don't check if the value is within the bounds.

correct_boundaries = False
while not correct_boundaries:
    lower = int(input("What is the lower bound? "))
    upper = int(input("What is the upper bound? "))
    if (lower >= upper):
        print("Lower bound must be less than upper bound.")
    else:
        number = random.randint(min(lower, upper), max(lower, upper))
        correct_boundaries = True


while (True):

        guess = int(input("What is your guess? "))
        if (guess < number):
            print("Nope, too low.")

        elif (guess > number):
            print("Nope, too high.")

        elif (guess == number):
            print("You got it!")
            break

        else:
            print("Guess should be between lower bound and upper bound.")

Merely move them out of the loop

import random

seedVal = int(input("What seed should be used? "))
random.seed(seedVal)

lower = int(input("What is the lower bound? "))
upper = int(input("What is the upper bound? "))
if (lower >= upper):
    raise ValueError("Lower bound must be less than upper bound.")

number = random.randint(min(lower, upper), max(lower, upper))

while (True):    
    guess = int(input("What is your guess? "))
    if (guess < number):
        print("Nope, too low.")
    elif (guess > number):
        print("Nope, too high.")
    elif (guess == number):
        print("You got it!")
        break
    else:
        print("Guess should be between lower bound and upper bound.")

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