0

The program below is just a high/low numbers game. One of the 4 criteria is having an error check to make sure the lower variable is in fact lower than the upper variable.

I've tried implementing it a few different ways to no success. I keep receiving a ValueError and I'm pretty certain it's because if I enter 10 as the "lower" variable and 1 as the "upper" variable, randint is trying to call randint(10, 1) instead of prompting the user an error and to reenter the numbers. I've tried a separate loop and if statements.

import random

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


lower = int(input('Enter lower bound. '))
upper = int(input('Enter upper bound. '))
answer = random.randint(lower, upper)


while True:
    guess = int(input('What is your guess? '))
    if guess < lower or guess > upper:
        print('Please enter a guess between lower bound and higher bound.')
    elif guess == answer:
        print('You got it!')
        break
    elif guess > answer:
        print('Nope, too high.')
    else:
        print('Nope, too low.')
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Cody
  • 3
  • 1
  • Use a try-except statement. Here is a useful tutorial: https://www.youtube.com/watch?v=NIWwJbo-9_8&t=5s – Leonardo Jan 23 '21 at 17:41
  • 1
    You appear to have understood the concept of `if` conditions and `while True`. You need to apply both before calling `randint` – DeepSpace Jan 23 '21 at 17:43
  • Essentially you need to use the same techniques that you have already used to implement the game logic, but instead with the two bounds the user has entered. – mkrieger1 Jan 23 '21 at 17:44
  • To insure lower <= upper, you can just use: `lower, upper = min(lower,upper), max(lower, upper)` before `answer = randint(lower, upper)`. – DarrylG Jan 23 '21 at 17:46

2 Answers2

2

You can just do:-

valid_numbers = False
while not valid_numbers:
    lower = int(input('Enter lower bound. '))
    upper = int(input('Enter upper bound. '))
    if lower > upper:
        print("the lower value should be smaller than the upper value.")
        continue
    else:
        break

random.randint(lower, upper)
Gurneet singh
  • 135
  • 1
  • 6
-1

You may want to use Exception handling. Simply raise an exception if values are not right, catch it and print message. Or catch the following error if this sequence is permited by default. Regards