简体   繁体   English

Python陷入无限While循环?

[英]Python stuck in infinite While loop?

First time python user here.这里是第一次使用 python 用户。 I am trying to use a while loop to set up a type of search tool for school courses.我正在尝试使用 while 循环为学校课程设置一种搜索工具。 I will get the prompts to insert the Subject and CatalogNbr;我将收到插入主题和 CatalogNbr 的提示; but instead of printing the course name (eg "Introduction to Research" from the first block) like I need, it immediately loops back to requesting inputs for the Subject and CatalogNbr again.但不是像我需要的那样打印课程名称(例如,第一个块中的“研究简介”),它立即循环回到再次请求主题和 CatalogNbr 的输入。

From what I have researched so far, I need to include break and continue statements between the loops, but everytime I try to include those I get syntax errors.根据我到目前为止的研究,我需要在循环之间包含 break 和 continue 语句,但是每次我尝试包含这些语句时都会出现语法错误。

Any help on how to accomplish this will be greatly appreciated任何关于如何实现这一点的帮助将不胜感激

    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!")

Because python cares about indentation, your while loop only loops over the first two lines, as the rest of the program is seen as supposed to execute after the while loop finishes.因为 python 关心缩进,你的while循环只循环前两行,因为程序的其余部分应该在 while 循环完成后执行。 Indenting all the code from the first if and forwards should fix your problem.从第一个if和 forwards 缩进所有代码应该可以解决您的问题。

your program should look like this.你的程序应该是这样的。 You need to take care about indentation when using python.使用python时需要注意缩进。

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!")

Your if and elif statements need to be indented.您的 if 和 elif 语句需要缩进。

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!")```


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM