What is the difference between a cycle "if" and "while"?

Both commands are used to run a chunk of code that satisfies the condition inserted. The main difference is that while is a loop, i.e. it is run until the condition is satisfied, while if stops in every case. The two commands have the same objective, however if is usually used when you know exactly how many times you want to test the condition.

FN

Related Python Mentoring answers

All answers ▸

Create an algorithm that can be used as a guessing game. Make sure to import random at the start.


What is the difference between & and && operators?


What would output this code? print(hello world)


Write a function s(n) that will return a list of the first n squares.