简体   繁体   中英

Edit your program which asks for a username. It should check that the first four characters are alpha and the fifth and sixth are digits. IN PYTHON

user = input("Enter your username (It should be of minimum 7 characters): ")
for a in user:
    for a in range(0,4):
        if (a.isalpha()) == True:
            print("The first four character are alphabet !")
        else:
            print("the first four characters are not alphabets !")
            break
for a in user:
    for a in range(4,6):
        if a.isdigit()==True:
            print("The fifth and sixth characters are digits !")
        else:
            print("The fifth and sixth characters are not digits !")
            break

a in user: will iterate through every characters user, and assign it to a each time. Remove your second for. That should work.

you can directly use the indexing method in string data types here like

if user[0].isalpha() == True and user[1].isalpha() == True and user[2].isalpha() == True  and user[3].isalpha() == True :
    print("The first four character are alphabet !")
else:
    print("the first four characters are not alphabets !")

and same for checking digits. You can also try loops to iterate the indexes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM