How do I make the numbers count down each time the user inputs something?

Viewed 40
while flag:
    x = input("Input name or press \'q\' to quit:\n")
    x = x.lower()
    for char in x:
        if char.isdigit():
            print("Your name cannot be a number. Please type your name again:\n")
            continue
    if x == "q":
        flag = False
    elif x == "abe":
        print("Welcome back, Abe")
        flag = False
    else:
        i = 4
        i-= 1
        while i > 0:
            print(f"Incorrect name. Please try again. You have {i} tries remaining.\n")
            i-= 1
            break
            if i == 0:
                print("You have run out of tries. System is locked.")
                break

This is something I was playing around with after I first started learning python a couple days ago. I understand why the number isn't changing but I can't think of a way to make the number different each time it asks for input. Also, if the user inputs a sequence of numbers and letters, the error messages repeat for each number in the input. How would I write it so that as soon as a number is detected, one error message is sent and the loop is restarted?

4 Answers

The only problem is you are defining i inside the loop. Instead you should define it outside.

Here is the code;

i = 4
flag = True
while flag:
    x = input("Input name or press \'q\' to quit:\n")
    x = x.lower()
    for char in x:
        if char.isdigit():
            print("Your name cannot be a number. Please type your name again:\n")
            continue
    if x == "q":
        flag = False
    elif x == "abe":
        print("Welcome back, Abe")
        flag = False
    else:
        i-= 1
        while i > 0:
            print(f"Incorrect name. Please try again. You have {i} tries remaining.\n")
            break
            if i == 0:
                print("You have run out of tries. System is locked.")
                break

Please comment if you need more explanation

How would I write it so that as soon as a number is detected, one error message is sent and the loop is restarted?

In the top part, you are iterating through every character of the string. So naturally, if the character is a digit, it will print the error message. Hence your problem.

You can change it to:

x = x.lower()
if not x.isalpha():
    print("Your name cannot be a number. Please type your name again:\n")
    continue

The str.isalpha() method checks if all characters of the string are alphabets.

I understand why the number isn't changing but I can't think of a way to make the number different each time it asks for input.

Please clarify this part!

Welcome to stackOverflow. I think you probably wanted to implement something like this:

i  =  4
while i>0:
    x = input("Input name or press \'q\' to quit:\n")
    x = x.lower()
    for char in x:
        if char.isdigit():
            print("Your name cannot be a number. Please type your name again.\n")
            break
    if x == "q":
        i = 0
    elif x == "abe":
        print("Welcome back, Abe")
        i = 0
    else:
        i -= 1
        if i:
            print(f"Incorrect name. Please try again. You have {i} tries remaining.\n")
        else:
            print("You have run out of tries. System is locked.")
            break

Let me know if you would like to add more functionality.

There are many things to improve in this code, I will list all the ones I could find and provide the corrected code.

  • Consider using a while True loop.
  • You needn't check each character in the input for isdigit(). Is digit does that already. For example, "foo_bar".isdigit() return False, "123".isdigit() returns True and "foo_123_bar".isdigit() returns False.
  • Use the break keyword to exit while loops (read more on break).
  • Define i = 4 before the loop starts, otherwise it gets re-initialized to 4 every time a wrong input is encountered and thus never changes.
  • You don't need a loop inside a loop to keep track of remaining tries (see code below).
# Number of tries
i = 4

while True:
  # Read and process input 
  x = input("Input name or press \'q\' to quit:\n")
  x = x.lower()

  # Check if input is digit
  if x.isdigit():
      print("Your name cannot be a number. Please type your name again:\n")
      continue

  # Check if input is 'quit'
  if x == 'q':
      break

  # Check if input is 'abe'
  elif x == 'abe':
      print('Welcome back, Abe')
      break

  # Wrong input, take appropriate action
  else:
    # User has tries remaining, warn user
    if i > 0:
      print(f'Incorrect name. Please try again. You have {i} tries remaining.\n')
      i -= 1

    # User has runout of tries, lock system
    else:
      print("You have run out of tries. System is locked.")
      break
Related