This particular number guessing game gives you a different answer whether you guess the number in 10 guesses or not. It’s working fine except, if you so happen to guess the correct number on the first try, the program terminates. I have included the print(num) line to be able to test the program more efficiently. I will, of course remove this before completion.
I tried a while loop and I was expecting, that if the 1st guess was correct, the program would continue but it terminates.
Here is the code:
import random
ans = 0
num = random.randrange(1, 1001)
count = 0
cont = ''
print(num)
ans = int(input('Guess my number between 1 and 1000 with the fewest guesses: '))
while ans != num:
if ans < num:
ans = int(input('Too Low. Try again: '))
count += 1
if ans > num:
ans = int(input('Too High. Guess again: '))
count +=1
if ans == num:
print('Congratulations. You guessed the number!')
count +=1
if count <= 10:
print('Either you know the secret or you got lucky!')
else:
print('You should be able to do better!')
cont = input('Would you like to play again? ("yes" or "no") ')
if cont == 'yes':
count = 0
num = random.randrange(1, 1001)
print(num)
ans = int(input('Guess my number between 1 and 1000 with the fewest guesses: '))
continue
else:
break