Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

python - Python-如何在嵌套的while循环中深入循环,以不返回到整个循环的开始,而是使其返回自己的while循环(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.

(它起作用,但是用于if循环。)

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.

(我要这样做,例如,当有人在第一个if语句循环中输入28时,它不会返回到开头,而只是问相同的问题。)

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.

(因此,对于年份,假设我输入10,它将再次要求年份,而不会将用户送回开始或终止程序。)

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?

(另外,当我尝试在def下运行代码,而没有def函数时,直接运行它,这使我无法从str转换为int错误,这意味着无法在title_year列和变量year之间进行比较,但是当我使用df.dtype title_year列是int32,而我的year变量又是int,为什么我不能同时比较它们呢?)

while x==1:
while True:
    try:
        choice = input("Welcome to movie recommender v1, please choose your genre first!

1 for Action
2 for Adventure
3 for Animation
4 for Biography
5 for Comedy
6 for Crime
7 for Documentary
8 for Drama
9 for Family
10 for Fantasy
11 for Film-Noir
12 for Game-Show
13 for History
14 for Horror
15 for Music
16 for Musical
17 for Mystery
18 for News
19 for Reality-TV
20 for Romance
21 for Sci-Fi
22 for Sport
23 for Talk-Show
24 for Thriller
25 for War
26 for Western

Enter your choice:")
        choice=float(choice)
        choice=round(choice)
        choice=int(choice)
    except ValueError:
        print("
------Not a number, enter a proper value------
")
        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?
Please enter a number between 0 and 10:")
            score=float(score)
            score=round(score)
        except ValueError:
            print("
------Not a number, enter a proper value------
")
            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("
------Not a number, enter a proper value------
")
                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
  ask by Ayberk Kum translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...