Question:
Python Help - Invalid Syntax Problem?
anonymous
2010-08-26 12:48:36 UTC
I have this code in python 3.1 from a tutorial and it keeps giving me an invalid syntax error on only one "self" command (its marked off with astrixes in the following code, the astrixes themselves are not in the code):

class vector(object):

def __init__(self, list1, list2):
self.diff=(list2[0]-list1[0], list2[1]-list1[1])
print *self*.diff

a =(20.0, 25.0)
b =(40.0, 55.0)
thing=vector(a,b)

Any idea what might be causing it?
Three answers:
om
2010-08-27 09:13:12 UTC
The syntax error is because of the 'print', not really anything to do with the 'self'.



The tutorial must have been written for Python 2. In Python 2 'print' is recognised as a statement and is treated specially.



In Python 3 'print' is no longer a statement, it's just an ordinary function. This means that its arguments must be enclosed in parentheses. Change that line to this:



  print ( self.diff )



and everything should be fine.



This is the Python 3 change that trips people up most often. There are a few other differences between Python 3 and Python 2, as discussed at the link below. You might want to read that page in case you run into some of those other issues as you progress through your tutorial.
anonymous
2016-10-22 07:40:24 UTC
the blunders message is imparting you with the clue that a variety of operands interior the expression i*t+i-c is a chain. Like a itemizing or a tuple. My first wager as to which and why is the two i or c and the in all danger reason is with the traces employing enter(). while introduced on for enter, in case you style some thing like 2,50 for the value for occasion, then enter() will study that as a tuple. it somewhat is why maximum programmers i've got encountered consistently use raw_input() as something you style is taken as a string, so it aspects a fixed beginning factor and you don't get hidden bugs. this is one self-explanatory thank you to deal with enter. valid = fake mutually as not valid:         item_cost = raw_input('what's the value of your merchandise?: ')         attempt:                 item_cost = drift(item_cost)                 valid = genuine         different than ValueError:                 print('invalid! please enter a decimal quantity')
?
2016-11-01 18:11:38 UTC
Astrixes


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...