【问题标题】:Python stuck in infinite While loop?Python陷入无限的While循环?
【发布时间】: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


【解决方案1】:

因为 python 关心缩进,你的 while 循环只循环前两行,因为程序的其余部分被视为应该在 while 循环完成后执行。缩进第一个if和转发的所有代码应该可以解决您的问题。

【讨论】:

  • 完美!这正是我所需要的!
【解决方案2】:

您的程序应如下所示。使用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!")

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-10
      • 2015-11-20
      相关资源
      最近更新 更多