python - 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
解决方案
推荐阅读
- tensorflow - 将 SMOTE 与 tensorflow 的 ImageDataGenerator Flow From Directory 一起使用
- azureservicebus - 如何查看服务总线主题中的第二组消息?
- linux - 比较两个文本文件,如果第二个文件的行包含第一个文件的两列,则删除该行
- javascript - Tailwindcss – 动态更新 UI 的最佳方式(无 JS 框架)
- mysql - OPENQUERY SQL Server MYSQL 更新
- alexa - 创建技能后如何做某事?
- java - 如果没有中断,Java 开关会运行所有情况
- azure - Azure TTS 神经语音音频文件以 1 字节大小异常创建
- python - 当我尝试安装 easy_install 电子邮件时,如何解决没有名为“email.MIMEMultipart”的模块?
- python - 如何让 Discord.py 获得许多用户对特定表情符号的反应?