There is an issue with the program when the user correctly guesses the number. The program should end when the break statement executes in the while loop found in main(), but instead it times out.
import random
def main():
level = get_level()
number = generate_random_number(level)
while True:
guess = get_guess()
if check_guess(number, guess) == False:
continue
else:
break
def get_level():
while True:
level = input("Level: ")
try:
int(level)
except ValueError:
continue
if int(level) <= 0:
continue
else:
return int(level)
def generate_random_number(level):
number = random.randint(1, level)
return number
def get_guess():
while True:
guess = input("Guess: ")
try:
int(guess)
except ValueError:
continue
if int(guess) <= 0:
continue
else:
return int(guess)
def check_guess(number, guess):
if guess > number:
print("Too large!")
return False
if guess < number:
print("Too small!")
return False
if guess == number:
print("Just right!")
return True
main()
Having multiple return statements in one function is a mistake. There shall ever be only one, unless that’s unworkable due to tons of checks.
Cringe! That’s like watching bad movies for the joy of really really bad movie moments. Watch
Dead Snow II
THENDead Snow I
. Both are cringe. Former good cringe later really really bad cringe. Do not watch in chronological order.A return statement within a while loop. Is that good or bad cringe?
Code with multiple return in one function/method screams noob. Especially when its completely unnecessary and avoidable. The return statement in random locations is a close 2nd.
The return statement in a while loop is just eyebrow raising. Like trying to write cringe, but forgot the threadpool, with GIL enabled, within the while on crack cocaine loop.