-1

Here is a simple program I'm attempting. The actual searches for capital letters work fine, my main problem is that when I run it, it only asks for the first and last name a second time after wrong input and doesn't do anything else with it. I want it to loop infinitely until isit_uppercase == True. What am I doing wrong?

import re
def name_get():
    global name
    name = input("First and last name: ")
    return name

name_get()

name_search = re.search(r'(.*) (.*)', name, re.M)
#separates first and last name
firstcap = name_search.group(1)
lastcap = name_search.group(2)

isit_uppercase = re.search(r'[A-Z]', name) #We want this to be true
lowercase_first = re.search(r'\b[A-Z].*', firstcap) #We want this to be true
lowercase_last = re.search(r'\b[A-Z].*', lastcap) #We want this to be true

#testing that the above code is working properly
print(isit_uppercase, "\n", lowercase_first,"\n", lowercase_last)

def main():
    if lowercase_first:
        print("Please capitalize your first name!")
        name_get()
    elif lowercase_last:
        print("Please capitalize your last name!")
        name_get()
    elif isit_uppercase == True:
        print("That's a nice name!")
    else:
        print("Please capitalize your first and last name.")
        name_get()


main()
while isit_uppercase == False:
    main()

I have googled a lot with no luck for answers that apply to this specific situation (that I know of).

Thank you in advance for your ideas!

Engineero
  • 12,340
  • 5
  • 53
  • 75
  • There are names like : `Someone von Brauke` .. more then 2 words, some of whom have to be lowercase. Read [Falsehoods Programmers Believe About Names](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) – Patrick Artner Jul 03 '19 at 19:07
  • If you really need it that way: `name = ' '.join(x.capitalize() for x in name.split(" ") )` would do it automatically... – Patrick Artner Jul 03 '19 at 19:08
  • I understand that some names include lowercase, this was for an assignment that specified the format. Thanks though – compuling Jul 07 '19 at 01:44

1 Answers1

0

OK, there's kind of a lot going on here. I'd recommend doing some programming tutorials online or something, but the following changes should fix your problem and help your code look a bit more pythonic.

First, there are better ways to do this, and as mentioned in the comments, not all names begin with an uppercase letter. For illustrative and hopefully educational purposes though, let's look at your code.

Basically you are only checking for uppercase once, when your script first runs, and never again. Put your input and checks in your main method:

def main():
    name = name_get()  # get the name first
    # Figure out upper/lowercase
    name_search = re.search(r'(.*) (.*)', name, re.M)
    #separates first and last name
    firstcap = name_search.group(1)
    lastcap = name_search.group(2)
    isit_uppercase = re.search(r'[A-Z]', name) #We want this to be true
    lowercase_first = re.search(r'\b[A-Z].*', firstcap) #We want this to be true
    lowercase_last = re.search(r'\b[A-Z].*', lastcap) #We want this to be true
    if lowercase_first:
        print("Please capitalize your first name!")
    elif lowercase_last:
        print("Please capitalize your last name!")
    elif isit_uppercase == True:
        print("That's a nice name!")
    else:
        print("Please capitalize your first and last name.")
    return isit_uppercase  # we'll use this to kill our while loop

Next, break your while loop using the value returned from main:

result = False  # initialize the value
while result == False:
    result = main()

And get rid of that global! You almost never need those.

def name_get():
    name = input("First and last name: ")
    return name
Engineero
  • 12,340
  • 5
  • 53
  • 75