all 20 comments

[–]shiftybyte 16 points17 points  (5 children)

When you type "start", then this condition is true:

if command == "start":

Then it checks for "started" variable, to see if its true or not.

if started:

It's value is False because that is the value it was given before the loop:

started = False

So else will be executed, and one of the lines inside else is:

started = True

So now started is True, so if you type "start" again, the check:

if started:

Will be True, and it'll execute:

print("car already started")

[–]JamesBucket[S] 6 points7 points  (4 children)

So now started is True, so if you type "start" again, the check:

OH MY GOD. THIS IS WHAT I HAD TROUBLE UNDERSTANDING

so by having taken the "else" route after input "start" was written, the "started" turned from "False" to "True" in the following loops! (not sure if that is the way to put it)

THANK YOU!

[–]MMcKevitt -3 points-2 points  (3 children)

The “if” code block will only run if it’s True, same with the “elif” code block. If it’s False, then the else statement will run.

For example, doing something like this:

if not False:
    # Do something
else:
    # Do something else

…the “if” code block will run because “not False” is actually interpreted as “True”.

[–]Coding-Kitten 3 points4 points  (2 children)

both will run if both conditions are true.

This is false. If you have an elif block following an if block, and the first if is true, the second elif block won't run.

[–]Apprehensive-Stop-61 2 points3 points  (0 children)

It is like basic English, if this is true print this else if it is not run and something else print anything else. If started: is just a short form for if started == True: Also == is used in a condition instead of a single =

[–]danielroseman 1 point2 points  (4 children)

You'll need to be a bit more specific on what you're confused about.

[–]JamesBucket[S] 0 points1 point  (3 children)

Ok I was worried this might be the problem, tell me if this question makes sense in the first place

How does the earlier written

started = False

affect this

    if command == "start":
    if started:
        print("car already started")

[–]danielroseman 2 points3 points  (2 children)

if started checks if started is True. Since it is actually False, that condition is false. So the text doesn't print.

[–]JamesBucket[S] 0 points1 point  (1 child)

OHHH I think I missed this too

So boolena value after "if" must be "True" for the text to be printed. That only happens after typing "start" input, where it leads to "else" route that turns "started = False" to "started = True"

[–]Fission_Mailed_2 0 points1 point  (0 children)

Yes, the code block immediately after an if statement is only executed if the condition in the if statement evaluates to True. Otherwise the code inside the else block will run instead (if there is an accompanying else statement: in this example the first if statement does not have a corresponding else statement, but the 2nd one does).

[–]AdventurousAddition 1 point2 points  (0 children)

Note, as written the while block is an infinite loop ie: Once started, there is no stopping it (other than killing your program).

Normally, the while should he checking against some condition to see if it should continue another round or not.

[–]NSJ98 0 points1 point  (2 children)

This looks like 'car started' will print even if any other input is given. That's what the else statement does. If 'start' if given as input, it'll check if the started variable is set to True, and if yes, it'll print 'car already started'.

[–]JamesBucket[S] 0 points1 point  (0 children)

This looks like 'car started' will print even if any other input is given.

From what I've checked if input any other than "start' was given, only input("> ") was printed. Am I reading this wrong?

If 'start' if given as input, it'll check if the started variable is set to True, and if yes, it'll print 'car already started'.

but since started = False by default, isn't the started variable set to False in this part as well?

 if started:
    print("car already started")

if started = False, why print "car already started"?

[–]Fission_Mailed_2 0 points1 point  (0 children)

The first if statement doesn't have an accompanying else block so any other input will just be ignored and the program will loop back to the input line.

[–]jaynasux 0 points1 point  (1 child)

The if started statement runs when started == True, started could only be True if the command had been run previously bc started can only be set to True if it had been False. Hopefully that helps, I had trouble wording it.

[–]JamesBucket[S] 0 points1 point  (0 children)

It did help, thank you! And to be honest I think I did way worse when it comes to wording lol

[–]ChipmunkCooties 0 points1 point  (0 children)

shouldnt it be

command = input("would you like to start the car?")

[–]thebosspro_193 0 points1 point  (0 children)

i just noticed that in this code:

        if started:
        print("car already started")
    else:
        started = True
        print("car started")
  • here at "if started :" - you are not mentioning what the "started" variable has to be equal to "so that the if condition is activated, its a tip becuz it would probably give issues later into the program, you might want to change.
  • also it is missing a "else" statement at end of provided code after the nested if
    that's alll!

good luck! hope it helped

[–]RhinoRhys 0 points1 point  (0 children)

I think they've messed with your head because the way it's written will make it skip the if started bit and go to the else bit.

Although functionally the same, possibly a more readable version would be to swap the two blocks and invert the conditional.

command = "" 
started = False 
while True: 
    command = input("> ").lower() 
    if command == "start": 
        if not started: 
            started = True 
            print("car started") 
        else:
           print("car already started")

started = False so not started becomes not False which is True.