简体   繁体   中英

I just made a simple guessing game, but my 'remaining guesses' var isn't working

I did a condition that if there's only one guess remaining, it shows 'one guess remaining', otherwise, it shows 'x guesses remaining". But no matters if there's 1 guess remaining, the variable which decides the condition is always 3.

sorry for the English by the way.

import random

words = ("elephant", "giraffe", "dog", "cat", "turtle", "bird")
s_word = random.choice(words)
guess = ""
g_limit = 3
g_count = 0
out_of_g = False
g_remain = g_limit - g_count

while guess != s_word and not out_of_g:
    if g_count < g_limit:
        if g_remain < 2:
            print(str(g_remain) + " guesses remaining")
        else:
            print(str(g_remain) + " guess remaining")
        guess = input("enter a word: ")
        g_count += 1
    else:
        out_of_g = True

if out_of_g:
    print("you lose")
else:
    print("you won")

The printouts will always show 3 guesses remaining because you don't update g_remain . You should do that after checking that the count is smaller than the limit:

#...
while guess != s_word and not out_of_g:
    if g_count < g_limit:
        g_remain = g_limit - g_count # <------ UPDATE HERE
        if g_remain < 2:
            print(str(g_remain) + " guesses remaining")
        else:
            print(str(g_remain) + " guess remaining")
        guess = input("enter a word: ")
        g_count += 1
    else:
        out_of_g = True
#...

Or if you think further you might be able to get rid of eather g_count or g_remain .

import random

words = ("elephant", "giraffe", "dog", "cat", "turtle", "bird")
s_word = random.choice(words)
guess = ""
g_limit = 3
g_count = 0
out_of_g = False
g_remain = g_limit - g_count

while guess != s_word and not out_of_g:
    if g_count < g_limit:
        if g_remain > 1:
            print(str(g_remain) + " guesses remaining")
            g_remain -= 1
        else:
            print(str(g_remain) + " guess remaining")
            g_remain -= 1
        guess = input("enter a word: ")
        g_count += 1
    else:
        out_of_g = True

if out_of_g:
    print("you lose")
else:
    print("you won")

Here is the solution. Change the if statement too, for correct version of 'guesses' and 'guess'.

your code has too many unnecessary vars, we should fix it first

import random
words = ("elephant", "giraffe", "dog", "cat", "turtle", "bird")
s_word = random.choice(words)
guess = ""

g_count = 4
for guess in range(3):
    print(str(g_count - 1) + " guesses remaining")
    guess = input("enter a word: ")
    if guess == s_word:
        print('You Won')
        break
    else:
        continue
    

g_remain is set once. you have to update the value in every stage. here's the solution:

while guess != s_word and not out_of_g:
    g_remain = g_limit - g_count

try this:

You need to change only one line g_remian variable should be declare after while loop then only its works properly.

here is the code.

import random

words = ("elephant", "giraffe", "dog", "cat", "turtle", "bird")
s_word = random.choice(words)
guess = ""
g_limit = 3
g_count = 0
out_of_g = False

while guess != s_word and not out_of_g:
    g_remain = g_limit - g_count
    if g_count < g_limit:
        if g_remain < 2:
            print(str(g_remain) + " guesses remaining")
        else:
            print(str(g_remain) + " guess remaining")
        guess = input("enter a word: ")
        g_count += 1
    else:
        out_of_g = True

if out_of_g:
    print("you lose")
else:
    print("you won")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM