Python陷入了无限While循环中?

问题描述 投票:-1回答:1

第一次使用python的用户。我正在尝试使用while循环为学校课程设置一种搜索工具。我将得到提示以插入Subject和CatalogNbr;但是没有像我所需要的那样打印课程名称(例如,从第一个块开始的“研究简介”),而是立即循环回到再次请求主题和CatalogNbr的输入。

根据到目前为止的研究,我需要在循环之间包含break和continue语句,但是每次尝试包含语法错误时都包含在内。

非常感谢您提供有关如何完成此操作的帮助

    Query = 'Y'

while Query == 'Y':
   Subject = input("Enter the Subject: \n> ")
   CatalogNbr= input("Enter the CatalogNbr: \n> ")
if Subject == 'LIBS' and CatalogNbr == '150':
        print(f"The title of {Subject,CatalogNbr} is Introduction to Research")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'SDEV' and CatalogNbr == '400':
        print(f"The title of {Subject,CatalogNbr} is Secure Programming in the Cloud")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'PHIL' and CatalogNbr == '348':
        print(f"The title of {Subject,CatalogNbr} is Religions of the East")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'BEHS' and CatalogNbr == '320':
        print(f"The title of {Subject,CatalogNbr} is Disability Studies")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'PSYC' and CatalogNbr == '354':
        print(f"The title of {Subject,CatalogNbr} is Cross-Cultural Psychology")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'SPCH' and CatalogNbr == '482':
        print(f"The title of {Subject,CatalogNbr} is Intercultural Communication")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'WMST' and CatalogNbr == '200':
        print(f"The title of {Subject,CatalogNbr} is Introduction to Womens Studies Women and Society")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'HYST' and CatalogNbr == '482':
        print(f"The title of {Subject,CatalogNbr}is History of Japan to 1800")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'ASDT' and CatalogNbr == '370':
        print(f"The title of {Subject,CatalogNbr} is Interpreting Contemporary China")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")

elif Subject == 'JAPN' and CatalogNbr == '333':
        print(f"The title of {Subject,CatalogNbr} is DJapanese Society and Culture")
        Query = input("\nWould you like to search for another title? (Y or N)\n> ")
else:
        print(f"I'm sorry {Subject,CatalogNbr} is not an avalible option.")

if Query == 'N':
    print("Thank you for using the Catalog Search!")
python-3.x while-loop nested-loops
1个回答
0
投票

由于python关心缩进,因此while循环仅在前两行循环,因为程序的其余部分被视为在while循环结束后执行。从第一个if开始缩进所有代码,应该可以解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.