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
#1
Photo 
Dear Friend,
I am a beginner in python.
I find it hard to understand the logic of the following issues:
1- why we use empty string?
2-Why when you right "start" for the second time you get "car already-started"

Your help will be appreciated.

Note: It is a code for a game. the code is long but I have selected the section that I did not understand.

command = "" 
already_started = False
while command != "quit":
   command = input(">").lower()
   if command == "start":
        if already_started:
            print("car already started")
        else:
            already_started = True
            print("car already started")
            print("car started")
Output:
>start car started >start car already started
buran write Sep-18-2023, 09:48 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
Python code: While loop with if statement - by HAMOUDA - Sep-18-2023, 09:25 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 744 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,070 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,721 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,940 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,475 May-04-2020, 06:34 AM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,244 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