【发布时间】:2020-02-07 22:22:44
【问题描述】:
第一次在这里使用python。我正在尝试使用 while 循环来为学校课程设置一种搜索工具。我将收到插入主题和目录Nbr 的提示;但不是像我需要的那样打印课程名称(例如第一个块中的“研究简介”),而是立即循环回到请求主题和 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!")
【问题讨论】:
-
你的缩进是关闭的 - 你所有的 ifs 都在循环之外,所以你不能摆脱它们。缩进很重要!
标签: python-3.x while-loop nested-loops