Having issues getting the correct output with hi low game

32 Views Asked by At

I have to let the player know if their guess is too low, too high, or correct, but I am not getting any output after asking them their bounds. Any help noting what I am doing wrong? This is for my current version of Python, I believe.

print('Welcome to the Higher or Lower game!')
import random
lowr = int(input('What would you like for your lower bound to be?: '))
upr = int(input('And your highest?: '))
x = (random.randint(lowr, upr))
if lowr >= upr:
    print('The lowest bound must not be higher than highest bound. Try again.')
    if lowr < upr:
        g = int(input('Great now guess a number between', lowr, 'and', upr, ':'))
    while g > x:
        int(input('Nope. Too high, Guess another number: '))
        while g < x:
            int(input('Nope. Too high, Guess another number: '))
            if g == x:
                print('You got it!')
2

There are 2 best solutions below

2
AsrtoMichi On

Here is a possible version of the correct code:

import random
print('Welcome to the Higher or Lower game!')
while True:
    lowr = int(input('\nWhat would you like for your lower bound to be?: '))
    upr = int(input('And your highest?: '))
    x = (random.randint(lowr, upr))
    if lowr >= upr:
        print('The lowest bound must not be higher than highest bound. Try again.')
    if lowr < upr:
        g = int(input(f"""Great now guess a number between
            {lowr} and {upr}:"""))
        while True:
            if g < x:
                g = int(input('Nope. Too low, Guess another number: '))
            elif g > x:
                g = int(input('Nope. Too high, Guess another number: '))
            if g == x:
                print('You got it!')
                break

There were some errors with: the use of the input method and the event lagestine.

0
cards On

Single loop and break-free version.

import random


print('Welcome to the Higher or Lower game!')

are_boundaries_fixed = False
is_guessed = False
while not (is_guessed and are_boundaries_fixed):

    if not are_boundaries_fixed:
        lowr = int(input('What would you like for your lower bound to be?: '))
        upr = int(input('And your highest?: '))

        if lowr >= upr:
            print('The lowest bound must not be higher than highest bound. Try again.')
        else:
            are_boundaries_fixed = True
            x = random.randint(lowr, upr)
            g = int(input(f'Great now guess a number between {lowr} and {upr}:'))
    else:
        if g > x:
            g = int(input('Nope. Too high, Guess another number: '))
        elif g < x:
            g = int(input('Nope. Too small, Guess another number: '))
        else:
            is_guessed = True
            print('You got it!')

Notice that input accepts a single parameter, the text to be displayed on the screen. So, 'Great now guess a number between', lowr, 'and', upr, ':' will raise an error, use instead string formatting, for ex f-string.