The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Y.G. Bae (36) [Avatar] Offline
#1
I feel that the initial value of the tries variable should be 1 instead of 0. By the initial value of the tries variable being 0, the first trial becomes "You tried to guess 0 times", and the user can enter the values 6 times though the user's last trial doesn't produce any response. I understand by changing the initial value of tries to 1, the last if condition also should become "tries <= max_tries" or "tries != max_tries+1" because the addition of tries variable is done after the nested if statement. Therefore, in my opinion, the code should be as below:
secret = "code"
max_tries = 5
guess = input("Guess a word: ")
tries = 1
while guess != secret:
    print("You tried to guess", tries, "times")
    if tries == max_tries:
        print("You ran out of tries.")
        break
    guess = input("Guess again: ")
    tries += 1
if tries <= max_tries:
    print("You got it!")
Ana Bell (33) [Avatar] Offline
#2
Yep, good catch. I will fix this.