I cannot figure out why the following statements dont work.
randomKey = random.choice(list(topic.keys()))
randomValue = random.choice(topic[randomKey])
current = "-" * len(randomValue)
while current != randomValue:
(statements)
else:
(statements)
However, if i alter the 1st line to
while current == randomValue:
the statement after ‘else’ executes correctly. Otherwise, the statement after ‘else’ does not execute. Any idea why what may be causing the strange behaviour? Full code has been excluded for it will run through this entire page.
It is part of the Python grammar. From the documentation:
So in the first case, it must be that the while condition never evaluates to false, while in the second it eventually does. Note that explicitly breaking out of the loop will not execute the else clause.