Home > front end >  how i break the while loop in func "loop of user"?
how i break the while loop in func "loop of user"?

Time:12-19

I need to break the while loop, I try to do it with status statment and it didn't work to me. Any suggestions what are the easiest ways to break a while loop? This is my code:

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()  

The I try to use in satatus like you see and write "break" in the else satement and it not working, it still in the loop and won't break. I will love some help here.

CodePudding user response:

You can put the while loop inside the function loop_of_user instead.

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()
  • Related