当用户输入错误时,如何统计无效输入的出现次数?

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

我想数一数“invalid input”这个词出现了多少次

这是我的代码。请帮助我。

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

为其分配一个计数器

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

只需添加一个计数器变量即可。

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