arx
arx

Reputation: 13

Python - (if/else) extra characters?

So I'm doing my homework but I can't seem to figure out how to add "extra" characters to regular statement.

I want this code to say that lname have to consist only from letters and " " or/and "-" symbols and can't start with " ". How can I achieve this? I don't even know what to search for or try. Isn't there some kind of “includes” function built in? I'm getting desperate.

def perenimi2():
    global lname
    lname = perenimi.get()

    if lname.isalpha():
            textVar.set("")
            nd2 = {"Perenimi": str(lname)}
            uusklient.update(nd2)
            post2()
            
    else:
        textVar.set ("Sisestage korrektne perekonnanimi!")

What I mean is how can I make IF statement that MUST include letters and may also include "-" or/and space. I want to make fname to be only letters and "-" or/and space. (This is for a name field which also can't start with space)

I'm sorry if this has been asked before but I haven't been able to find the solution.

-----CODE AFTER USING POSTED ANSWER-----
With the posted answer I found a new problem, it now allows input or entry to be empty which can't be empty.

I translated everything to English.

import string


###FirstName

def firstname1():
    global fname
    allowed = " -"
    cantStart = " "
    fname = firstname.get()
    
    if (set(fname) <= set(allowed + string.ascii_letters) and not fname.startswith(cantStart)):
        textVar.set("")
        familyname2()
    
    elif fname == "":
        textVar.set("Insert correct name!")

        
    else:
        textVar.set("Insert correct name!")
        
###FamilyName
def familyname2():
    global lname
    lname = familyname2.get()
    allowed = " -"
    cantStart = " "
    empty = ""
    
    if (set(lname) <= set(allowed + string.ascii_letters) and not lname.startswith(cantStart)):
        textVar.set("")
        post2()
        
    elif lname == "":
        textVar.set("Insert correct family name!")

    else:
        textVar.set("Insert correct family name!")


firstname1()

Upvotes: 1

Views: 101

Answers (1)

MarianD
MarianD

Reputation: 14191

import string

ALLOWED = " -"
CANT_START = " "

if (set(lname) <= set(ALLOWED + string.ascii_letters) 
        and not lname.startswith(CANT_START)
        and lname):

No elif branch (i.e. remove it from your code).


The explanation:

string.ascii_letters is "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

set(lname) is the set of all characters in lname, e. g. if lname == "abba", then set(lname) will be "ab".

The <= operation between two sets means “is a subset of”, i.e. if all elements in the left set exist in the right set, too.

lname is True only for nonempty strings (you may also use lname != "", but using only lname is more Pythonic).

The long expression in the if branch is surrounded with parentheses only for the possibility to split it freely into more lines, i.e. without the necessity to end every non-complete line with the continuation symbol (\). Otherwise, it has to be in the form as

if set(lname) <= set(ALLOWED + string.ascii_letters) \
       and not lname.startswith(CANT_START)          \
       and lname:

(or be written fully only in one, very long line).

Upvotes: 1

Related Questions