-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)
james.peng
  • 373
  • 1
  • 3
  • 13
  • 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. – N. Ivanov Feb 13 '18 at 09:15
  • Your list elements are [strings, not integers](https://www.tutorialspoint.com/python3/python_strings.htm). – Mr. T Feb 13 '18 at 09:16
  • The key problem is not string and int,but a error occur after these codes typyed. I do know string in loop. – james.peng Feb 13 '18 at 09: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 before the final `print`). – cdarke Feb 13 '18 at 09:21
  • The answer that we expect is "Houston and Houston" ,not ''SyntaxError: invalid syntax" – james.peng Feb 13 '18 at 09:23

2 Answers2

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
>>>
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • Yes,that's the reason.Thank you.Sorry for my bad expression. – james.peng Feb 13 '18 at 09:30
  • 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! – cdarke Feb 13 '18 at 09:32
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?

BoboDarph
  • 2,751
  • 1
  • 10
  • 15