1

I am using python 3.6.5 I want to input a variable which can take in name of the person with space. This is my code for it right now:

def addition():
    with open('Directory.csv','a',newline='') as file:
        w=csv.writer(file,delimiter=',')
        def names():
            name=input('Enter Name     :')
            n=0
            for i in name:
                if i.isalpha() or i.isspace():
                    n+=0
                else:
                    n+=1
            if n==0:
                return name
            else:
                print('Error')
            return names()
        name=names()

But when I press enter without any value inputted, it still accepts it:

Enter Value of k     :     2
Enter Name     :
Enter Year of Birth     :

What should be my code to correct this?

  • 1
    I'd suggest removing the irrelevant portions. We don't need to see your I/O, and your printed output doesn't match your code. – Mad Physicist Dec 09 '19 at 17:53
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Dec 09 '19 at 17:56
  • @Matthias. While that's good advice, it's completely irrelevant to the actual question. – Mad Physicist Dec 09 '19 at 17:59
  • Yes! Thank You. Found a solution through that using length of string inputted. – Ivin Georgi Dec 09 '19 at 17:59
  • 1
    @IvinGeorgi. I've added a regex solution too – Mad Physicist Dec 09 '19 at 18:19
  • You might find the answer to the question [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) useful. – martineau Dec 09 '19 at 18:53

1 Answers1

1

The basic problem here is that the loop for i in name: won't run if name is empty, leaving n at zero. The least invasive way to fix this is to check in the subsequent if:

if name and n == 0:

There are other improvements I'd recommend, starting with removing the recursive call in favor of a loop. A user could trigger a stack overflow by typing in bad characters enough times.

Rather than counting bad characters, you can break out of the loop early. This removes your dependence on n entirely:

def names():
    while True:
        name = input('Enter Name: ')
        if name:
            for i in name:
                if not i.isalpha() or not i.isspace():
                    break
            else:
                return name
        print('Error')

The else clause here is correctly matched to the for loop. It's a neat python trick for running code only if the loop didn't break.

There are less manual ways to check that a string contains only letters and spaces. The most popular likely being regular expressions, which are supported by the standard library. This would be a simple regex:

import re

...

name_pattern = re.compile('[a-zA-Z ]+')

...

def names():
    while True:
        name = input('Enter Name: ').strip()
        if name_pattern.fullmatch(name):
            return name
        print('Error')
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264