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()
The code looks like it should run fine. How are you executing it and what makes you think it "times out"?
It's for CS50P which uses a customized VS Code. It has an automated code checker which I ran when I was done.
How is that checker configured?
It might be doing something like this:
and because you're already invoking
main
as the module is imported, it's getting stuck the second time around. Maybe add some indicativeprint
at the entrypoint to your main function.Another reply in here has supplied the standard idiom for making a module executable:
The idiom allowed it to pass the checker's tests! Thanks for your help!
You picked up an STD from mvirts. That dodgy terminology has been passed on and added to your lexicon.
South Park suggested the cure for this, eat a banana. Life doesn't have to make sense, roll with it.
Quickly taking a shower was oddly never suggested.