Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The for loop doesn't loop even when the answer is not right

The problem may be somewhere in the end,the for loop stops and the input is asked

while enter!="off":
    if enter == "1":
        prefer= input("enter your preference")
        if prefer =="sports":
            print("Hardcore Sports Podcast")
            enter = input('Enter 1 - recommendation, 2 - draw, off - exit')
        else:
            print("Kanye West's new album")
            enter = input('Enter 1 - recommendation, 2 - draw, off - exit')
    if enter=="2":
        band=input("Enter the name of the band")
        for word in range(3):
            if band=="Queen":
                print("You win a concert ticket!")
                break
    enter = input('Enter 1 - recommendation, 2 - draw, off - exit')  ```
           

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The for loop actually does loop, it just does do anything unless the answer is correct. you want:

while enter!="off":
    if enter == "1":
        prefer= input("enter your preference")
        if prefer =="sports":
            print("Hardcore Sports Podcast")
            enter = input('Enter 1 - recommendation, 2 - draw, off - exit')
        else:
            print("Kanye West's new album")
            enter = input('Enter 1 - recommendation, 2 - draw, off - exit')
    if enter=="2":
        for word in range(3): #swapped this line
            band=input("Enter the name of the band") #with this line
            if band=="Queen":
                print("You win a concert ticket!")
                break

    enter = input('Enter 1 - recommendation, 2 - draw, off - exit') 

Just swap line 11 with line 12 so that the input call is in the loop.

I think a slight improvement would be to use:

while enter!="off":
    if enter == "1":
        prefer= input("enter your preference")
        print("Hardcore Sports Podcast") if prefer =="sports" else print("Kanye West's new album")
    if enter=="2":
        for _ in range(3):
            band = input("Enter the name of the band")
            if band == "Queen":
                print("You win a concert ticket!")
                break
    enter = input('Enter 1 - recommendation, 2 - draw, off - exit')  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading