Python 陷入无限 While 循环?
Python stuck in infinite While loop?
第一次python用户在这里。我正在尝试使用 while 循环为学校课程设置一种搜索工具。我会得到插入 Subject 和 CatalogNbr 的提示;但它并没有像我需要的那样打印课程名称(例如 "Introduction to Research" 来自第一个块),而是立即循环返回以再次请求输入 Subject 和 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 关心缩进,你的 while
循环只循环前两行,因为程序的其余部分被认为应该在 while 循环完成后执行。缩进第一个 if
和向前的所有代码应该可以解决您的问题。
您的程序应该如下所示。使用 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!")
您的 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!")```
第一次python用户在这里。我正在尝试使用 while 循环为学校课程设置一种搜索工具。我会得到插入 Subject 和 CatalogNbr 的提示;但它并没有像我需要的那样打印课程名称(例如 "Introduction to Research" 来自第一个块),而是立即循环返回以再次请求输入 Subject 和 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 关心缩进,你的 while
循环只循环前两行,因为程序的其余部分被认为应该在 while 循环完成后执行。缩进第一个 if
和向前的所有代码应该可以解决您的问题。
您的程序应该如下所示。使用 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!")
您的 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!")```