Python Forum
Problem with a 'break' statement.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with a 'break' statement.
#1
I have indented and have tried moving the 'break' statement around, but I keep receiving this error. What am I doing wrong? Thank you!

prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "
while True:
	city = input(prompt)
if city == 'quit':
	break
else:
	print(f"I'd love to go to {city.title()}!")
Error:
File "main.py", line 6 break ^ SyntaxError: 'break' outside loop
Reply
#2
Clearly, your if and else statements aren't insider the loop. Lines 5 and 8 should be indented at the same level as line 4 and so on.
Reply
#3
Hi,

Maybe you want to examine the indentation starting line 5.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#4
Your indentation from line 5 is wrong - they aren't in the loop
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with while statement. BobSmoss 3 1,666 Jan-08-2022, 03:22 PM
Last Post: BobSmoss
  Problem in if-else statement shantanu97 2 2,437 Apr-09-2021, 06:37 AM
Last Post: shantanu97
  multiple condition if statement problem FelixReiter 3 2,600 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
  Problem with If statement and dataframe Milfredo 1 1,774 Sep-16-2020, 05:50 AM
Last Post: Milfredo
  Problem with If else statement Milfredo 5 2,592 Aug-30-2020, 06:32 PM
Last Post: Milfredo
  Problem with an IF statement Ryan_Todd 13 5,017 Jan-30-2020, 08:22 PM
Last Post: snippsat
  Problem with 'and' in 'if' statement CoderMan 3 2,535 Oct-06-2019, 07:32 PM
Last Post: buran
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,210 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Problem with elif statement Haddal99 2 2,277 May-20-2019, 09:26 AM
Last Post: avorane
  if statement and in operator problem bobger 5 4,024 Nov-30-2017, 06:50 PM
Last Post: bobger

Forum Jump:

User Panel Messages

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