-3

My college found this problem
How to run this script correctly,without error message 'wrong synax found'?
He used windows-cmd and type 'python' and then type these code:

>>> for i in ['London','NewYork','Houston']:  
...   print(i)  
...   i=i+' and '+i 
... print(i)
5
  • 1
    why would you increment the counter inside a for loop, especially even when the list has strings and not ints? That is done automatically. Looks you are a beginner, so instead of asking low quality questions on SO, I would advise on getting your hands on a python tutorial. Commented Feb 13, 2018 at 9:15
  • Your list elements are strings, not integers. Commented Feb 13, 2018 at 9:16
  • The key problem is not string and int,but a error occur after these codes typyed. I do know string in loop. Commented Feb 13, 2018 at 9:18
  • 2
    Did you really get "wrong synax found"? When reporting a problem, be careful to give the exact error message. More likely you got SyntaxError: invalid syntax, and this is because in interactive mode you need a blank line after the loop (just hit <return> before the final print). Commented Feb 13, 2018 at 9:21
  • The answer that we expect is "Houston and Houston" ,not ''SyntaxError: invalid syntax" Commented Feb 13, 2018 at 9:23

2 Answers 2

1

The problem is that you are using python interactively:

>>> for i in ['London','NewYork','Houston']:  
...     print(i)
...     i=i+' and '+i 
... print(i)
  File "<stdin>", line 4
    print(i)
        ^
SyntaxError: invalid syntax

Python needs to know that the loop has ended, so you insert a blank line:

>>> for i in ['London','NewYork','Houston']:  
...     print(i)
...     i=i+' and '+i 
...                                           # <<<< blank line
London
NewYork
Houston
>>> print(i)
Houston and Houston
>>>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes,that's the reason.Thank you.Sorry for my bad expression.
We would have got to the answer much quicker if you had copied and pasted your whole session, including the error. You are a beginner, so we forgive you provided you learn next time you post. Have fun!
0

In order for your script to run "correctly" we need to know what the expected behavior of your script is. In other words, we need the implementer's intent behind the code. Offtopic QA rant: Devs, beware, code will not always explain itself. You'll save yourself a lot of debugging with a few lines of comments.

Now back to your original question.

You post a list of python commands. To me it looks like you try to iterate through a list of strings, print them and then increase the value of the index in your for loop.

Problem here is that you python will not allow you to simply add an integer to a string object. This piece of code here i+=1 means you are trying to add the integer 1 to the string '1'. Your python interpreter will fail with a TypeError here.

Without full disclosure of intent, this seems like an invalid piece of code that was probably caused by you not understanding how for loops, objects and operators work in Python. I would suggest starting to read up on each subject.

Secondly, you mention a reference to a "wrong syntax" error. No piece of the code you listed would trigger a SyntaxError. Unless we had a piece of crucial information you didnt list: the version of python interpretor you use. See in python 2.7 print was called with print 'ABC' while in 3.0 and above the print statement changed to a function and it's called with print('ABC'). This could indeed trigger a SyntaxError when called in 2.7 interpretor.

Later update

The answer that we expect is "Houston and Houston"

What is the logic that would trigger this expected result? What are you trying to achieve?

3 Comments

Using python under cmd requires a blank line after the loop.I found cdarke 's answer is right.Thanks for your help.
I'm starting to think answering to people that don't take the time to write a proper question is a waste of time.
Just want to recur this problem easily

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.