Home > Software design >  How do I count the number of occurrences of the invalid input when the user enter the wrong input?
How do I count the number of occurrences of the invalid input when the user enter the wrong input?

Time:10-23

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

CodePudding user response:

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))

CodePudding user response:

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