Python Forum
Python code: While loop with if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python code: While loop with if statement
#2
Your questions have nothing to do Python. They are questions about the program logic. Program logic can be arbitrary. Everything here is a guess.

1. What value do you think should be used to initialize command?

The loop is designed to run until the user enters "quit". When command == 'quit' the loop stops running. The comparison in line 3 requires that a variable named "command" has been created. Variables are created by assigning a value to a name, so there has to be a value assigned to command before the loop. It could be almost any value. It could be None, or 2 or "Hello". The only thing you cannot do is set command = "quit" because the loop would not run.

2. The code you posted is not the code you ran to get the output

The code prints "car already started" regardless of the value of "already started". I think the code was written like this:
        if already_started:
            print("car already started")
        else:
            already_started = True
            print("car started")
This is the code I will use to answer the question.

I don't know. I do not know why the author of the code wants the program to print "car already started" if you enter "start" after you have already started the car. The program uses already_started to remember that you had entered "start" sometime in the past. When you enter "start" it checks if the car was already_started and prints "car already started" if already_started is True, else it prints "car started" and assigns alreadhy_started = True to remember that the car was started.
HAMOUDA likes this post
Reply


Messages In This Thread
RE: Python code: While loop with if statement - by deanhystad - Sep-18-2023, 11:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 741 Sep-05-2023, 12:50 PM
Last Post: ToniE
  code won't advance to next statement MCL169 2 775 Apr-11-2023, 09:44 PM
Last Post: Larz60+
  Multiply and Addition in the same loop statement with logic. joelraj 2 1,068 Feb-02-2023, 04:33 AM
Last Post: deanhystad
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,717 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Compare each element of an array in a logic statement without using a for loop leocsmith 3 5,936 Apr-01-2021, 07:57 PM
Last Post: deanhystad
  how to create pythonic codes including for loop and if statement? aupres 1 1,950 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  if statement in for loop researcher123 6 2,659 Oct-01-2020, 05:07 PM
Last Post: deanhystad
  Unable to understand a statement in an existing code ateestructural 1 2,259 Aug-01-2020, 09:38 PM
Last Post: deanhystad
  Help: list comprehension for loop with double if statement mart79 3 2,474 May-04-2020, 06:34 AM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,243 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020