0

So I'm confused on how to make the invalid input appear for only number inputs, how to turn the inputs into a list, and how to increase the count for every input? I genuinely tried but I just can't figure it out :( .

This is the question:

Exercise #17c: Lists #3

  1. You’re going to take the same idea from a previous exercise that asked the user for multiple numbers and until the user entered "done".

  2. On the very top of your program, you need to assign (=) an empty list ([]) to a variable (line above the While True loop)

  3. The program is going to continuously ask for a list of names.

  4. Once the user types in ‘done’, your program should then display how many names were entered and display the final list. An example is shown below of what yours should look like

EXAMPLE:

Enter a name: Mrs. Brown

Enter a name: Ms. Bliss

Enter a name: 3

That’s not a name! TRY AGAIN!

Enter a name: Mr. Kent

Enter a name: done

There are 3 names in the list [‘Mrs. Brown’, ‘Ms. Bliss’, ‘Mr. Kent’]

This is what is have so far:

namesList = []
while True:
  name = input("Please enter a name, then press enter. When finished type done: \n")
  if name == "done":
    namesList = namesList[:-1]
    print("There are " + str(len(namesList)) + " names in the list " + str(namesList))
    break
  else:
    continue
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41

4 Answers4

0

This is how you are going to add the names given in your list

namesList = []
while True:
  name = input("Please enter a name, then press enter. When finished type done: \n")
  if name == "done":
    print("There are " + str(len(namesList)) + " names in the list " + str(namesList))
    break
  else:
    namesList.append(name) #append method adds all the given names in the end of the list
    continue
Phoenix Doom
  • 77
  • 10
0

You have a tricky question about what is a name and what is not a name. (https://www.bbc.com/news/magazine-36107590)

But, in any case, the requirements you were provided do not clearly define what is to be accepted, rejected. The example you have indicates that digits are to be rejected.

There are several ways you can achieve that. One way you can check for specific patterns in python is with regular expressions (regex). You can search for that here and identify robust methods for pattern matching.

In this case, if the only requirement is to reject digits, you can also make use of the fact that python will generate an error if you request to convert text to an integer. You can make use of that fact to distinguish between all-digits and not-all-digits in a character string.

This example below shows how you can do that using a try/except block (also a python paradigm).

ls_test_names = ['Brooke', 'Raj2', '567', '3', '3.141592654']
for t in ls_test_names:
    try:
        name = int(t)
        print('%s looks like digits to me' % t)
    except ValueError as ve:
        print('ValueError : %s' % str(ve))
        print('%s : is not all digits' % t)
        name = t
ValueError : invalid literal for int() with base 10: 'Brooke'
Brooke : is not all digits
ValueError : invalid literal for int() with base 10: 'Raj2'
Raj2 : is not all digits
567 looks like digits to me
3 looks like digits to me
ValueError : invalid literal for int() with base 10: '3.141592654'
3.141592654 : is not all digits
bici-sancta
  • 172
  • 1
  • 11
  • A name is considered anything with letters. Its just that if a number is inputted it is invalid. Anything else is valid. – Brooke Gibson Feb 21 '21 at 16:21
  • Maybe there is some flexibility in how this is interpreted. But, you can see that the names you already have in your list have periods in the salutation ... so those are not letters. Similarly, there are locations where names (and usernames !) are hyphenated or have diacritical marks: Zoë Toussaint-Zhang ... and many other possibilities. You can build robust test cases for whatever are your requirements, typically with regular expressions. There are some more suggestions here : `https://stackoverflow.com/questions/19859282/check-if-a-string-contains-a-number` – bici-sancta Feb 21 '21 at 17:31
0

Try this:

def hasNumbers(inputString):
  return any(char.isdigit() for char in inputString)

namesList = []
while True:
  name = input("Please enter a name, then press enter. When finished type done: \n")
  if hasNumbers(name):
    print ("That’s not a name! TRY AGAIN!")
    continue
  else:
    if name == "done":
      print("There are", len(namesList), "names in the list", namesList)
      break
    else:
      namesList.append(name)
      continue
Akansha
  • 116
  • 6
0

You can check for numbers in a string using any() and a comprehension to detect inputs that are not names. The .isdigit() method tells you if a string (or character) if formed of only numbers. The .isalpha() method is similar for letters.

names = []
name  = None
while name != "done":
    if name : names.append(name)             # add to list if name present
    name = input("Enter a name: ")           # get a name
    if sum(c.isalpha() for c in name) < 2 \
    or any(c.isdigit() for c in name):       # 2+ letters, no numbers
        name = print("That's not a name! TRY AGAIN!") # sets name to None
print("There are",len(names),"names in the list",names)

BTW, the else: continue in your code is not needed given that you are not doing anything in the loop afterward and the break statement on the if will not go there either. Also, as a general rule, you should make an effort to avoid using break/continue when possible. Those are often an indication that you are not in complete control of your variable states and hint at unforeseen side effects.

Alain T.
  • 40,517
  • 4
  • 31
  • 51