Python raw_input 从字典中提取

我正在尝试实现一个解决方案,在该解决方案中我调用 displayPerson(),该解决方案接受用户输入的 ID 号,并为用户打印信息。我应该注意,我正在从 Internet 下载一个包含以下格式数据的 csv 文件:


ID,姓名,生日

1,杰克·斯派洛,2000 年 9 月 20 日


我的目标是从用户那里获取一个数字,该数字将查找并显示 ID。我希望提示继续显示并要求用户输入一个数字,直到他们输入负数或 0 退出。


    loop= True

    while loop== True:

        prompt = int(raw_input(" Enter ID of person you would like to search for: "))

        break


    if prompt >0:


        displayPerson(prompt,persons)



    else:

        print("Terminated.")

就目前而言,如果用户输入 1-100 之间的正数,我能够获得正确的输出,但是当我希望它向用户询问另一个数字时程序停止并且我无法理解如何工作这样它就会给用户一条消息,输入另一个小于 101 的数字(而不是给我一个 KeyError),显示数据,然后要求另一个数字。如果用户输入零,它会给出“终止”消息,然后停止,但我发现很难做任何其他事情。


这里是 displayPerson() 函数供参考:


def displayPerson(id, personDataDictionary):

    """


    :param id:

    :param personDataDictionary: Look at displayPerson in __name__ function. But I thought bday was pieces[2].

    :return:

    """

    print("id = {}, name = {}, birthday = {}".format(id, personDataDictionary[id][0],

                                                     personDataDictionary[id][1].strftime("%Y-%m-%d")))


德玛西亚99
浏览 224回答 1
1回答

阿波罗的战车

解决方案while True:&nbsp; &nbsp; prompt = int(raw_input(" Enter ID of person you would like to search for: "))&nbsp; &nbsp; if prompt > 100:&nbsp; &nbsp; &nbsp; &nbsp; print("Please try again")&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp;&nbsp; &nbsp; elif prompt < 1:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; displayPerson(prompt,persons)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; breakprint("Terminated.")解释您进入循环并从用户那里获得一个 ID。如果prompt > 0:显示人否则,跳出循环并打印“已终止”。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python