繁体   English   中英

我如何打破 func“用户循环”中的 while 循环?

[英]how i break the while loop in func "loop of user"?

我需要打破 while 循环,我尝试用状态声明来做,但它对我不起作用。 任何建议打破 while 循环的最简单方法是什么? 这是我的代码:

def loop_of_user(my_details):
    """_summary_
    the function do a variety of actions on the the variable my_detailes
        :my_details (dict): dictionary of detailes on mariha
    """
    num = int(input())
    if num == 1:
        print(my_details["first_name"])
    elif num == 2:
        print(my_details["birth_date"][3:5])
    elif num == 3:
        print(len(my_details["hobbies"]))
    elif num == 4:
        print(my_details["hobbies"][-1])
    elif num == 5:
        my_details["hobbies"].append("cooking")
        print(my_details["hobbies"])
    elif num == 6:
        print(tuple_birth_date(my_details["birth_date"]))
    elif num == 7:
        my_details ["age"] = calculate_age(my_details["birth_date"])
        print(my_details["age"])
    else:
        return "break"
        

def main():
    mariah_details = {"first_name" : "mariah", "last_name" : "carey", "birth_date" : "27.03.1970", "hobbies" : ["sing", "compose", "act"]}
    status = ""
    while status != "break":
        loop_of_user(mariah_details)

if __name__ == "__main__":
   main()  

我尝试像您看到的那样在 satatus 中使用并在 else 语句中写下“break”但它不起作用,它仍在循环中并且不会中断。 我会喜欢这里的一些帮助。

您可以将 while 循环放在函数loop_of_user中。

def loop_of_user(my_details):
    """_summary_
    the function do a variety of actions on the the variable my_detailes
        :my_details (dict): dictionary of detailes on mariha
    """
    while True:
        num = int(input())
        if num == 1:
            print(my_details["first_name"])
        elif num == 2:
            print(my_details["birth_date"][3:5])
        elif num == 3:
            print(len(my_details["hobbies"]))
        elif num == 4:
            print(my_details["hobbies"][-1])
        elif num == 5:
            my_details["hobbies"].append("cooking")
            print(my_details["hobbies"])
        elif num == 6:
            print(tuple_birth_date(my_details["birth_date"]))
        elif num == 7:
            my_details["age"] = calculate_age(my_details["birth_date"])
            print(my_details["age"])
        else:
            break


def main():
    mariah_details = {"first_name": "mariah", "last_name": "carey", "birth_date": "27.03.1970",
                      "hobbies": ["sing", "compose", "act"]}

    loop_of_user(mariah_details)


if __name__ == "__main__":
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM