Erstmaliger Python-Benutzer hier. Ich versuche, eine while-Schleife zu verwenden, um eine Art Suchwerkzeug für Schulkurse einzurichten. Ich werde aufgefordert, den Betreff und CatalogNbr einzufügen. Anstatt jedoch den Kursnamen (z. B. "Einführung in die Forschung" aus dem ersten Block) wie gewünscht auszudrucken, werden sofort wieder Eingaben für den Betreff und CatalogNbr angefordert.

Nach dem, was ich bisher recherchiert habe, muss ich break- und continue-Anweisungen zwischen die Schleifen einfügen, aber jedes Mal, wenn ich versuche, diese einzuschließen, erhalte ich Syntaxfehler.

Jede Hilfe, wie dies erreicht werden kann, wird sehr geschätzt

    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
Prophet 8 Feb. 2020 im 01:22

3 Antworten

Beste Antwort

Da sich Python um das Einrücken kümmert, durchläuft Ihre while - Schleife nur die ersten beiden Zeilen, da der Rest des Programms nach Abschluss der while-Schleife ausgeführt werden soll. Das Einrücken des gesamten Codes ab dem ersten if und vorwärts sollte Ihr Problem beheben.

0
The Zach Man 7 Feb. 2020 im 22:27

Ihr Programm sollte so aussehen. Bei der Verwendung von Python müssen Sie auf Einrückungen achten.

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!")
0
Daniel 7 Feb. 2020 im 22:33

Ihre if- und elif-Anweisungen müssen eingerückt werden.

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


0
Gerry P 7 Feb. 2020 im 23:02