Python Forum
Beginner Code, how to print something after a number of turns (guessing game)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Code, how to print something after a number of turns (guessing game)
#1
Hey guys,
I'm a beginner at coding and I'm trying to create this game where a person has to guess the word and only has 3 guesses. Now my simple problem is that, I want to interrupt the while loop so that after 2 guesses, it prints out "You only have 1 guess remaining"
I've tried a few ways but I still can't figure out how to do it. Any tips would be great :)

secret_word = "giraffe"   # this is the secret word
guess = ""                # variable to store the words
guess_count = 0
guess_limit = 3
out_of_guesses = False
two = 3
ifTwo = False


while guess != secret_word and not(out_of_guesses):   #if they havent guessed the word yet and have not ran out of guesses, then keep looping.
    if guess_count < guess_limit:
        guess = input("Enter Guess:")
        guess_count += 1
    #elif guess_count == 2:
    #     print("Last quess")

    else:
        out_of_guesses = True

if out_of_guesses:
    print("Unlucky mate, out of guesses.")
else:
    print("You've guessed correctly!")
Reply
#2
you could put a separate if statement in just before asking for input so the output would be
Output:
Enter Guess:1 Enter Guess:2 Last guess Enter Guess:3 Unlucky mate, out of guesses.
Reply
#3
(Jun-18-2020, 04:34 PM)Yoriz Wrote: you could put a separate if statement in just before asking for input so the output would be
Output:
Enter Guess:1 Enter Guess:2 Last guess Enter Guess:3 Unlucky mate, out of guesses.

Like this?
while guess != secret_word and not(out_of_guesses):   #if they havent guessed the word yet and have not ran out of guesses, then keep looping.
    if guess_count < guess_limit:
        #guess = input("Enter Guess:")
        guess_count += 1
    if guess_count == 2:
        #guess = input("Enter Guess:")
        print("last guess")    
    else:
        out_of_guesses = True
When I enter an input, it jumps straight to
Output:
Unlucky mate, out of guesses
Reply
#4
You would need to indent it so it becomes a stand alone inner if statement.
Go back to how you had the code and make this an inner if statement.
Reply
#5
(Jun-18-2020, 04:56 PM)Yoriz Wrote: You would need to indent it so it becomes a stand alone inner if statement.
Go back to how you had the code and make this an inner if statement.

I see, it works, thank you so much! I didn't know that indention matters a lot! :)
Took me awhile to figure out such a simple problem
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  needing some help to write some code for a game calculator rymdaksel 1 358 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  Beginner: Code not work when longer list raiviscoding 2 765 May-19-2023, 11:19 AM
Last Post: deanhystad
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,035 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  help me simple code result min and max number abrahimusmaximus 2 870 Nov-12-2022, 07:52 AM
Last Post: buran
  Beginner Higher Lower Game wallytan 2 1,541 Sep-29-2022, 05:14 PM
Last Post: deanhystad
  Print Player Name and Number of Pokemon webmanoffesto 1 1,069 Aug-23-2022, 08:54 PM
Last Post: deanhystad
  Problem with my pong game code Than999 8 3,763 May-15-2022, 06:40 AM
Last Post: deanhystad
Question Beginner Boolean question [Guessing game] TKB 4 2,227 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Unable to count the number of tries in guessing game. Frankduc 7 1,848 Mar-20-2022, 08:16 PM
Last Post: menator01
  Even number code syntax error MrCeez 1 2,248 May-02-2021, 06:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020