首页 > 解决方案 > Python - How to make a loop deep inside nested while loops to not return to the beginning of the whole loop but make it return to its own while loop

问题描述

Below is my code. I used try to catch the errors when the user inputs a string instead of an integer. It worked but for the if loops. I want to make it so when someone enters 28 for example for the first if statement loop, it does not return to the beginning but just ask for the same question. So for year lets say i enter 10 it would ask again for the year again and not send the user back to the beginning or terminate the program. I am creating a project and this is the beginning of my project.

Also, when i try to run the code under def , without the def function, run it direcly, it gives me can not convert from str to int error meaning can not compare between the title_year column and my variable year but when i check it with df.dtype title_year column is int32 and again my year variable is an int so why can't i compare them both?

while x==1:
while True:
    try:
        choice = input("Welcome to movie recommender v1, please choose your genre first!\n\n1 for Action\n2 for Adventure\n3 for Animation\n4 for Biography\n5 for Comedy\n6 for Crime\n7 for Documentary\n8 for Drama\n9 for Family\n10 for Fantasy\n11 for Film-Noir\n12 for Game-Show\n13 for History\n14 for Horror\n15 for Music\n16 for Musical\n17 for Mystery\n18 for News\n19 for Reality-TV\n20 for Romance\n21 for Sci-Fi\n22 for Sport\n23 for Talk-Show\n24 for Thriller\n25 for War\n26 for Western\n\nEnter your choice:")
        choice=float(choice)
        choice=round(choice)
        choice=int(choice)
    except ValueError:
        print("\n------Not a number, enter a proper value------\n")
        continue
    else:
        break
if 0<=choice<=26:
    genre=allgenres[choice-1]
    while True:
        try:
            score = input("What is the minimum imdb score you are looking for?\nPlease enter a number between 0 and 10:")
            score=float(score)
            score=round(score)
        except ValueError:
            print("\n------Not a number, enter a proper value------\n")
            continue
        else:
            break
    if 0<=score<=10:
        while True:
            try:
                year = input("Please enter the minimum production year limit:")
                year=float(year)
                year=round(year)
            except ValueError & (1990<=year<=2019):
                print("\n------Not a number, enter a proper value------\n")
                continue
            else:
                break
        if 1800<=year<=2100:
            def recommend_me_movie(genre,score,year,):
                genre=str(genre)
                score=float(score)
                print(df.loc[(df['imdb_score']>=score)&(df['genres'].str.contains(genre, flags=re.I, regex=True))&(df['title_year'>=year])])

            x=0
            break
        else:
            print("Error. Bad production year. Try from the beginning.")
            x=0
    else:
        print("Error. Bad imdb score. Try from the beginning.")
        x=0
else:
    print("Error. Bad choice. Try from the beginning.")
    x=0

标签: pythonloopsnested

解决方案


推荐阅读