Python Forum
Using break to exit a loop help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using break to exit a loop help
#1
Hi all, I'm having a problem trying to run this program. I was messing around with a similar program I tried to create and it would not work. So i took this one right out of my book and I the get the same outcome. Any help would be great ty
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("id love to go to " + city.title() + "!")
Please enter the name of a city you have visited:
(enter 'quit' when you are finished.) tbay
Traceback (most recent call last):
  File "input.py", line 30, in <module>
    city = input(prompt)
  File "<string>", line 1, in <module>
NameError: name 'tbay' is not defined
Reply
#2
I assume you are using Python 2. Use raw_input() instead of input() function. In Python 2 input() return value is evaluated so if it's not an expression Python look at it as a variable. But there is not such a variable defined so you get an error.

Better switch to Python 3
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Hi wavic ty for your help. You are right.. I thought I was using python 3.6. I have it downloaded and installed.. I also have the path set in visual studio code to python 3.6 .. Python 2.7 comes preinstalled with my mac, I'm going to have figure out how to remove it.. thank you very much

hmm I just read its a bad ideal to remove preinstall python 2.7 from my mac. I will have to try and figure out whats going on.. I know i added python 3.6 path to VS Code but no sure what to do now.

Python: Select Interpreter command from the Command Palette and i selected 3.6 but I'm still getting the same error when i run program without using raw_input function, not sure if anything else i should be doing will look for more info.

update..

ok when i use terminal in VS code i have to type "python3.6 plus file" and it works using 3.6.
Is there away i can use terminal without typing python3.6 or is this something i just have to do all the time?

thanks
Reply
#4
I have never used Mac but one way is to make the script executable. So you just cd /to_directory and run ./the_script. In order to do that the shebang line must be placed on top of the script: #!/usr /bin/env python3. This is for Linux and I am not sure if it will be the same for the Mac OS.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Ok great ty again wavic
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,764 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Code won't break While loop or go back to the input? MrKnd94 2 906 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  How to break out of a for loop on button press? philipbergwerf 6 1,663 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  break out of for loop? User3000 3 1,384 May-17-2022, 10:18 AM
Last Post: User3000
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 2,603 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  While loop doesn't exit KenHorse 3 1,967 Jun-20-2021, 11:05 PM
Last Post: deanhystad
  tkinter control break a while loop samtal 0 2,351 Apr-29-2021, 08:26 AM
Last Post: samtal
  Cannot 'break' from a "for" loop in a right place tester_V 9 3,890 Feb-17-2021, 01:03 AM
Last Post: tester_V
  How to break a loop in this case? Blainexi 10 7,158 Sep-24-2020, 04:06 PM
Last Post: Blainexi
  Exit Function - loop Tetsuo30 2 2,025 Sep-17-2020, 09:58 AM
Last Post: Tetsuo30

Forum Jump:

User Panel Messages

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