Let's combine loops with random numbers! We'll create a number guessing game.
The break keyword can exit a loop early:
while True:
print('This runs once')
break # exits the loop
And the += operator is shorthand for addition:
count = 0
count += 1 # same as count = count + 1
Use a while loop. Generate random number with random.randint(). Compare guess to number. Give hints like 'Too high!' or 'Too low!'. Count the attempts!
⚠️ Try solving it yourself first — you'll learn more!
while True:
print('This runs once')
break # exits the loop