(Python) multiple counters in a for loop?

Question:

trying to have multiple counters, where if you get a question right or wrong, it’s respective counter will go up by one.

The code is janky and not really the simplest way to do certain things (I think), as I’m new to coding.

import random
wordBank=["throw"]
word=random.choice(wordBank)
letters=list(word)

print("Welcome to Hangman!")
print()

for i in range(5):
  rightGuess=0
  wrongGuess=0
  print(wrongGuess,"/ 10 Wrong Guesses Used")
  answer=input("Choose a letter: ")
  if answer in letters:
    print("There's a",answer,"!")
    rightGuess+=1
  else:
    print("There's no",answer,"!")
    wrongGuess+=1

Here’s my entire code. I want the wrongGuess counter to go up by 1 every time the user incorrectly guesses, and the rightGuess counter to go up by 1 whenever the user correctly answers, but it’s not doing that.

Asked By: Archaniac

||

Answers:

Slight modification to your code, rightGuess, wrongGuess initializations to 0 should be outside the for loop.

import random
wordBank=["throw"]
word=random.choice(wordBank)
letters=list(word)

print("Welcome to Hangman!n")
rightGuess=0
wrongGuess=0
for i in range(5):
    print(wrongGuess,"/ 10 Wrong Guesses Used")
    answer=input("Choose a letter: ")
    if answer in letters:
        print("There's a",answer,"!")
        rightGuess+=1
    else:
        print("There's no",answer,"!")
        wrongGuess+=1
Answered By: M H

Now you reset the counter every pass of the cycle,
I think you wanted the counter not to reset.
If I understood you correctly then this is the solution:


wordBank=["throw"]
word=random.choice(wordBank)
letters=list(word)

print("Welcome to Hangman!")
print()

rightGuess=0
wrongGuess=0

for i in range(5):
  print(wrongGuess,"/ 10 Wrong Guesses Used")

  answer=input("Choose a letter: ")
  
  if answer in letters:
    print("There's a",answer,"!")
    rightGuess+=1

  else:
    print("There's no",answer,"!")
    wrongGuess+=1```
Answered By: Ilya Gorunov
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.