How do I count the number of occurrences of the invalid input when the user enter the wrong input?

Viewed 101

I would like to count how many times the words "invalid input " appears.

Here's my code. Please assist me.

while True:
    number = input("Enter a number: ")
    if number.isdigit():
        print("The input is valid.")
    else:
        print("Invalid input.")
        continue
2 Answers

Assign a counter for it

count = 0
while True:
    number = input("Enter a. number: ")
    if number.isdigit():
        print("The input is  valid.")
    else:
        print("Invalid input.")
        count += 1
        continue
print('Invalid responses count: {}'.format(count))

Simply add a counter variable.

i = 0
while True:
    number = input("Enter a number: ")
    if number.isdigit():
        print("The input is valid.")
    else:
        print("Invalid input.")
        i = i + 1  # Increment the counter variable
Related