你如何在 Python 中使用数组输入用户的字符列表

我正在尝试构建一个基本程序,该程序将从用户那里获取名称,并将显示包含 5 个以上字母的名称数量。


from array import*


list=array('u',[])


n=int(input("Enter the number of names first"))


for i in range(0,n):


    y=input("Enter the names one by one")

    list.append(y)

但是当运行此代码时,我收到一个错误


“类型错误:数组项必须是 unicode 字符”




喵喵时光机
浏览 256回答 2
2回答

不负相思意

继续评论,保持简单以便您理解:n = int(input("Enter the number of names first"))nameLst = []           # an empty list to store the desired namesfor i in range(0,n):    y = input("Enter the names one by one")         if len(y) > 5:               # if the len of name is > 5        nameLst.append(y)        # append it to the listfor i in range(len(nameLst)):    # iterate over the len of the list    print(nameLst[i])            # print all the names in the list

三国纷争

n=int(input('enter times : '))name =list(map(str, input("enter name spe by space : ").strip().split()))if len(name)==n:    name2 =[i for i in name if len(i)>=5]    print("name are ->", *name2)"""output enter times : 3enter name spe by space : john edward philipsname are -> edward philips"""
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python