为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

喵喵时光机 2021-12-21 16:15:21
我正在尝试构建一个基本程序,该程序将从用户那里获取名称,并将显示包含 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 字符”
查看完整描述

2 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

继续评论,保持简单以便您理解:


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


nameLst = []           # an empty list to store the desired names

for 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 list


for i in range(len(nameLst)):    # iterate over the len of the list

    print(nameLst[i])            # print all the names in the list


查看完整回答
反对 回复 2021-12-21
?
三国纷争

TA贡献1804条经验 获得超7个赞

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 : 3


enter name spe by space : john edward philips

name are -> edward philips


"""


查看完整回答
反对 回复 2021-12-21
  • 2 回答
  • 0 关注
  • 255 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号