Search code examples
pythonloops

Record how many times a while loop runs? - python


Imagine this:

You have a while loop

You want to know how many times it ran

What should you do???

Now I hear you say, what is the context?

Context: I am writing this program in Python which thinks of a number between 1 and 100, and you are to guess it. The guessing takes part in a while loop (please have a look at the code below) but I need to know how many guesses are made.

So, this is what I need:

print("It took you " + number_of_guesses + " guesses to get this correct.")

This is the complete code at Gist: https://gist.github.com/anonymous/1d33c9ace3f67642ac09

Please remember: I am using Python 3x

Thanks in advance


Solution

  • count = 0
    
    while x != y::
       count +=1 # variable will increment every loop iteration
       # your code
    
    
    print count