How to nest a while true loop inside of a while loop

Question: I am trying to have the code terminate when the counter hits 6 attempts. I thought I could achieve this with line 2 and 37 but the code keeps running until all the blanks have been guessed correctly.

ChatGPT suggests adding this line inside of each while True loop :

 if counter > 6:
        break  # Terminate the loop if counter exceeds 6

and changing line 2 to the following:

while counter <=6 

I’m wondering if there is a way to achieve my goal with my initial approach?

Appreciate the help.

counter = 1
while counter != 6:
  while True:
    dream = input("It was all a ______: ")
    if dream == "dream":
        print("Great you got it")
        break
    else:
        print()
        print("Try again")
        print()
        counter += 1
  print()
  while True:
    word = input("I used to read _____ up magazine: ")
    if word == "word":
        print("Nice! Keep going")
        break
    else:
        print()
        print("Try again")
        print()
    counter += 1
  print()
  while True:
    peppa = input("Salt and ______ and Heavy D up in my limouzine: ")
    if peppa == "peppa":
        print("You got it!")
        break
    else:
        print()
        print("Try again")
        print()
    counter += 1
  print()
  print("Nice! You got it! It took you", counter, "tries")
  counter += 1


Hi @HamzaCYYZ , welcome to the forums!
Could you format you code with this:```?

No, this is not a solution

Can you send the link to your repl?

My apologies, I thought I did. I was trying to edit my original post but it doesn’t look like I can.

I’m reproducing the code here:

counter = 1
while counter != 6:
  while True:
    dream = input("It was all a ______: ")
    if dream == "dream":
        print("Great you got it")
        break
    else:
        print()
        print("Try again")
        print()
        counter += 1
  print()
  while True:
    word = input("I used to read _____ up magazine: ")
    if word == "word":
        print("Nice! Keep going")
        break
    else:
        print()
        print("Try again")
        print()
    counter += 1
  print()
  while True:
    peppa = input("Salt and ______ and Heavy D up in my limouzine: ")
    if peppa == "peppa":
        print("You got it!")
        break
    else:
        print()
        print("Try again")
        print()
    counter += 1
  print()
  print("Nice! You got it! It took you", counter, "tries")
  counter += 1

Hi there! I fixed your code:

counter = 1
while counter != 6:
  while True:
    dream = input("It was all a ______: ")
    if dream == "dream":
        print("Great you got it")
        break
    else:
        print()
        print("Try again")
        print()
        counter += 1
  print()
  while True:
    word = input("I used to read _____ up magazine: ")
    if word == "word":
        print("Nice! Keep going")
        break
    else:
        print()
        print("Try again")
        print()
    counter += 1
  print()
  while True:
    peppa = input("Salt and ______ and Heavy D up in my limouzine: ")
    if peppa == "peppa":
        print("You got it!")
        break
    else:
        print()
        print("Try again")
        print()
    counter += 1
  print()
  print("Nice! You got it! It took you", counter, "tries")
  break

I changed your last line to break instead of counter+=1.

@HamzaCYYZ You can mark my above post as the Solution if it helps you.

@HamzaCYYZ Does the code that I provided help?

I believe this does what you ask and it uses a generic function where the question loop is and a game loop using this function in a match statement.
I tried to write the code in a readable (not necessarily efficient) way so that you might understand it

def ask_question(question,answer, counter, counter_limit=6, right_answer="Great you got it", wrong_answer="Try again"):
    while counter < counter_limit:
      ans = input(question)
      if ans == answer:
         print(right_answer)
         return counter
      else:
         print(wrong_answer)
         counter += 1
    return counter

counter = 1
question = 1
while counter < 7 and question < 3:
  match(question):
    case 1:
        counter = ask_question(question = "It was all a ______: ",
                               answer = "dream",
                               counter=counter,
                               counter_limit=6,
                               right_answer="Great you got it",
                               wrong_answer="Try again")
        print()
        question += 1
    case 2:
        counter = ask_question(question = "I used to read _____ up magazine: ",
                               answer = "word",
                               counter=counter,
                               counter_limit=6,
                               right_answer="Nice! Keep going",
                               wrong_answer="Try again")
        print()
        question += 1

Please note that the code does not do any case control (among other things), hence it is just an example.

2 Likes

*does not.

Anyway, what’s with the indents? Also, is match, case like the switch, case in js? (You learn something new everyday)

1 Like

If you think this is not doing what the original code failed to do, I suggest to look again.
Good luck.

Regarding the questions. The indents is a copy and paste thing and yes match is like switch, python has it since few 3.x versions ago.

1 Like

I don’t believe they were saying the code did not work, I believe they were correcting this:

1 Like

I appreciate the help, however this doesn’t solve what I’m trying to do.

I was hoping to use line 2 and 37 in my original code to limit the number of incorrect guesses. So the goal was to terminate the code if the user made 6 incorrect guesses.

When I ran your solution, I was still able to make unlimited incorrect guesses. I’m not sure what the last break statement is targeting in your solution, it doesn’t seem to be tied to any of the while True loops.

Just for some context, I’m super new to coding. I’m on day 20 of the 100 days to code for python course so I may be overlooking something super obvious.

I appreciate this.

I tried running the code but kept receiving syntax errors.

I’m really new to coding at the moment and don’t recognize a lot of the functions & statements in your solution so I may be overlooking something simple.

Probably idents as I copied from VSC and they got messed up or you have an old version.

1 Like

ok, so I decided to keep things simple and follow ChatGPT’s suggestion. I ended up deleting the second and last lines from my original code:

2nd line: while counter !=6
last line: counter +=1

I then added an elif statement to each of my while True loops:

while True:
    dream = input("It was all a ______: ")
    if dream == "dream":
        print("Great you got it")
        break
    elif counter == 6:
        print("Sorry, too many attempts.")
        exit()
    else:
        print()
        print("Try again")
        print()
        counter += 1

This allows me to accomplish the goal of terminating the code when the users makes 6 incorrect guesses.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.