Python Forum
Using break to exit a loop help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Using break to exit a loop help (/thread-6804.html)



Using break to exit a loop help - JJG - Dec-08-2017

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



RE: Using break to exit a loop help - wavic - Dec-08-2017

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


RE: Using break to exit a loop help - JJG - Dec-09-2017

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


RE: Using break to exit a loop help - wavic - Dec-09-2017

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.


RE: Using break to exit a loop help - JJG - Dec-09-2017

Ok great ty again wavic