[Python] Guessing game with while loop

 This time we're going to make Guessing game.. with While Loop that we learned last time.


print("Food name")
secret_word = "pasta"
guess = ""

I made secret_word

while guess != secret_word:
    guess = input("Enter guess: ")
    print("Try more~")

print("You win!")

!= means "not equal"

so if the guess is not equal to secret_word, it's going to continue to loop through 

Let's play

Food name
Enter guess: pizza
Try more~
Enter guess: sushi
Try more~

and when the guess is right 

You win!


You can set up "limit" to make it more fun

guess_count = 0
guess_limit = 3
out_of_guesses = False

I make count, limit, and out of guess

while guess != secret_word and not(out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Enter guess: ")
        guess_count += 1
        print("try more~")
    else:
        out_of_guesses = True

if out_of_guesses:
    print("Out of Guesses, you lose")
else:
    print("You win!")

so if guess is not equal to "secret_word and out_of_guesses", keep looping until count_limit


if out of guesses -> print("Out of Guesses, you lose")

if guess is right ->    print("You win!")


like this..

Food name
Enter guess: pizza
try more~
Enter guess: sushi
try more~
Enter guess: ramen
try more~
Out of Guesses, you lose






Popular posts from this blog

[Python] Dictionary

[Visual Design 2/3]

[JavaScript] For loop , Function