开发者社区> 问答> 正文

Python字符串输入循环

我正在寻找创建一个python函数,该函数将根据用户输入的内容一次遍历一个字符串。

假设清单为[hello,jello,mello]。该程序将打印出字符串,然后询问用户“是否要我阅读?”,然后单击“ y”,它将打印第一个元素,然后循环遍历字符串,直到用户输入“ n”,然后循环将停止。

谢谢!

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 08:58:46 430 0
1 条回答
写回答
取消 提交回答
  • 这应该工作:

    l = ["Hello", "Jello", "Mello"]
    for element in l:
        while True:
            answer = input("Do you want to read (y/n)")
            if answer.lower() == "y" or answer.lower() == "n":
                break
            else:
                print("Invalid input")
        if answer.lower() == "y":
            print(element)
        elif answer.lower() == "n":
            break
    

    希望这可以帮助!

    回答来源:stackoverflow

    2020-03-24 08:58:53
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载