Guessing minigame telling wrong

Asked

Viewed 76 times

0

Total hits and kicks do not appear correct, if I change "hits" and "totalguess" to 0 the first hit or kick counts as 0 and wanted as 1

from random import randint
acertos = 1
totalguess = 1
while acertos != 10:
    x = randint(1, 5)
    chute = int(input('Enter your guess'))
    if chute == x:
        print('Congratz! \n' + str(acertos) + ' Hits ')
        totalguess += 1
        acertos += 1
    else:
        print('Not quite! \nTry again! \n' + 'Attempts: ' + str(totalguess))
        totalguess += 1
print('End Game!\n' + 'Hits:' + str(acertos) + '\nAttempts: ' + str(totalguess))

https://i.stack.Imgur.com/4Ggt9.png

  • To solve the problem is not only initialize acertos and totalguess with 0 and update the value (totalguess += 1 and acertos += 1) before printing the messages?

  • Thank you very much!

1 answer

0


There are two causes for problems in your code:

  • The total hits are being printed before being incremented;
  • Variables are not being initialized with 0, to be incremented when needed.

Solving these problems, the code stays like this:

from random import randint

acertos = 0
totalguess = 0

while acertos < 10:
    x = randint(1, 5)
    chute = int(input('Enter your guess: '))

    if chute == x:
        acertos += 1
        totalguess += 1

        print('Congratz! \n' + str(acertos) + ' Hits ')


    else:
        print('Not quite! \nTry again! \n' + 'Attempts: ' + str(totalguess))
        totalguess += 1

print('End Game!\n' + 'Hits:' + str(acertos) + '\nAttempts: ' + str(totalguess))

And it works properly.

  • shoow, thank you very much bro, I’m not believing that my problem was so obvious so kkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.