[Tutor] help
Hi Team, I am learning puthon and trying the following code. But getting the following error. Please help me in knowing the code in better way. OS Linux Python version 2.7.13 def demo(s, exclaim): #""" # Returns the string 's' repeated 3 times. # If exclaim is true, add exclamation marks. result = s + s + s if exclaim: result = result + '!!!' return result def main(): print demo('Yay', False) ## YayYayYay print demo('Woo Hoo', True) ## Woo HooWoo HooWoo Hoo!!! Error: ./python2.py: line 1: syntax error near unexpected token `(' ./python2.py: line 1: `def demo(s,exclaim): Regards Sonia ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
On 05/02/2019 12:32, Sonia Miglani wrote: > OS Linux > Python version 2.7.13 Can you tell us how you are creating the file? Which editor are you using? It looks like there may be some spurious characters in your file. > def demo(s, exclaim): > #""" > # Returns the string 's' repeated 3 times. > # If exclaim is true, add exclamation marks. > > > result = s + s + s > if exclaim: > result = result + '!!!' > return result > > > def main(): > print demo('Yay', False) ## YayYayYay > print demo('Woo Hoo', True) ## Woo HooWoo HooWoo Hoo!!! It all works perfectly for me. > Error: > ./python2.py: line 1: syntax error near unexpected token `(' > ./python2.py: line 1: `def demo(s,exclaim): Notice the odd backtick (`) characters? They aren't in your code above, did you retype it or actually copy/paste your real code? It is important that you post the actual code you get the error from. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
Sonia Miglani wrote: > Hi Team, > > I am learning puthon and trying the following code. > > But getting the following error. > > Please help me in knowing the code in better way. > > > OS Linux > Python version 2.7.13 > > > > def demo(s, exclaim): > #""" > # Returns the string 's' repeated 3 times. > # If exclaim is true, add exclamation marks. > > > result = s + s + s > if exclaim: > result = result + '!!!' > return result > > > def main(): > print demo('Yay', False) ## YayYayYay > print demo('Woo Hoo', True) ## Woo HooWoo HooWoo Hoo!!! > > > Error: > ./python2.py: line 1: syntax error near unexpected token `(' That is not a Python error, that's a complaint of your shell. If you make a Python script executable you also have to insert the proper hash-bang line. In the case of Python 2 #!/usr/bin/python2 will probably work. Example shell session: $ cat tmp.py def demo(): print "heureka" demo() $ ./tmp.py ./tmp.py: line 1: syntax error near unexpected token `(' ./tmp.py: line 1: `def demo():' $ cat tmp2.py #!/usr/bin/python2 def demo(): print "heureka" demo() $ ./tmp2.py heureka $ > ./python2.py: line 1: `def demo(s,exclaim): > > > > > > Regards > Sonia > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
>> Error: >> ./python2.py: line 1: syntax error near unexpected token `(' > > That is not a Python error, that's a complaint of your shell. > If you make a Python script executable you also have to insert the proper > hash-bang line. In the case of Python 2 > > #!/usr/bin/python2 > > will probably work. Example shell session: > > $ cat tmp.py > def demo(): > print "heureka" > > demo() > $ ./tmp.py > ./tmp.py: line 1: syntax error near unexpected token `(' > ./tmp.py: line 1: `def demo():' or, of course, run it with python explicitly: $ python tmp.py ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help
On 05/02/2019 14:15, Peter Otten wrote: >> Error: >> ./python2.py: line 1: syntax error near unexpected token `(' > > That is not a Python error, that's a complaint of your shell. Oh, good catch Peter. I never noticed the start of the line I just read the text and saw the weird backtick... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor