You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python社交账号开发:如何从输入变量提取并打印前3个字符串?

解决仅打印社交媒体资料前3项的问题

直接调整代码的打印逻辑就能实现需求,以下是修改后的完整代码:

print('👻 👻  Welcome to SnapChat 👻 👻')
print('\n')

profile = input("Enter your profile details in this format (first_name,job_title,current_state_you_live_in,email_id,phone_number): ")
prof = profile.split(",")

print('\n')
print('这是你的资料展示给他人的样式')

# 仅遍历前3项信息
for x in prof[:3]:
    print(x, '\n')

关键修改说明:

  1. 替换原提示文本为需求指定的“这是你的资料展示给他人的样式”
  2. 用列表切片prof[:3]获取输入信息的前3项,替代原代码遍历整个列表的逻辑,确保只打印姓名、职业、居住州
  3. 把重复的print(' ')简化为print('\n'),代码更简洁

内容的提问来源于stack exchange,提问作者titan_coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 03:23:10