Reputation: 33
I am learning Python online on YouTube. And I have came across a piece of code that is confusing me. I would really appreciate if someone could help me out. Here is the code:
command = ""
started = False
while True:
command = input("> ").lower()
if command == "start":
if started:
print("car already started")
else:
started = True
print("car started")
What I didn't understand is how does Python execute double if
s? And how does it know it is already executed once and if typed start again it would give me another message. Any help would highly appreciated.
Upvotes: 3
Views: 87
Reputation: 4537
Here's an attempt to "transcribe" the code in English:
Upvotes: 1
Reputation: 545
The if
s are nested. The second if
and else
are only checked if the first condition is true. This is apparent, since they are indented after the first if
.
The first if
checks if the command is to start. If it is, then it checks if the car has already been started. If it has, there is no need to start it again. If not, then it starts the car.
Upvotes: 3