Py学习  »  Python

Python陷入无限While循环?

Prophet • 5 年前 • 1390 次点击  

从目前为止的研究来看,我需要在循环之间包含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社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54266
 
1390 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Gerry P
Reply   •   1 楼
Gerry P    5 年前

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


Daniel
Reply   •   2 楼
Daniel    5 年前

你的程序应该是这样的。使用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!")
The Zach Man
Reply   •   3 楼
The Zach Man    5 年前

因为python关心缩进,所以 while 循环只循环前两行,因为程序的其余部分被认为应该在while循环完成后执行。从第一个缩进所有代码 if