Python For Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack python exception class Overflow the company Business Learn more about hiring developers or posting ads with us python exception message Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a python custom exception community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Invalid syntax in “for item in L” loop up vote 4 down vote favorite I
Python Raise Valueerror
have a feeling I'm missing something pretty simple here but, in this one function: def triplets(perimeter): triplets, n, a, b, c = 0 #number of triplets, a, b, c, sides of a triangle, n is used to calculate a triple L = primes(int(math.sqrt(perimeter)) #list of primes to divide the perimeter for item in L: #iterate through the list of primes if perimeter % item == 0: #check if a prime divides invalid syntax for loop python the perimeter n = perimeter / item a = n**2 - (n+1)**2 #http://en.wikipedia.org/wiki/Pythagorean_triple b = 2n*(n+1) c = n**2 + n**2 if a+b+c == perimeter: #check if it adds up to the perimeter of the triangle triplets = triplets + 1 return triplets I am getting the error: for item in L: ^ SyntaxError: invalid syntax For completeness my entire program looks like this: import math def primes(n): #get a list of primes below a number if n==2: return [2] elif n<2: return [] s=range(3,n+1,2) mroot = n ** 0.5 half=(n+1)/2-1 i=0 m=3 while m <= mroot: if s[i]: j=(m*m-3)/2 s[j]=0 while j This module never needs to be imported explicitly: the exceptions are provided in the built-in namespace as well as the exceptions module. For class python exception stack trace exceptions, in a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived). Two exception classes that are not related via subclassing are never equivalent, even if they have the same name. The built-in exceptions listed below http://stackoverflow.com/questions/8886730/invalid-syntax-in-for-item-in-l-loop can be generated by the interpreter or built-in functions. Except where mentioned, they have an "associated value" indicating the detailed cause of the error. This may be a string or a tuple containing several items of information (e.g., an error code and a string explaining the code). The associated value is the second argument to the raise statement. https://docs.python.org/2/library/exceptions.html If the exception class is derived from the standard root class BaseException, the associated value is present as the exception instance's args attribute. User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition "just like" the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error. The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions. The following exceptions are only used as base classes for other exceptions. exception BaseException¶ The base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes (for that, use Exception). If str() or unicode() is called on an instance of this class, the representation of the argument(s) to you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps the most https://docs.python.org/3/tutorial/errors.html common kind of complaint you get while you are still learning Python: >>> while True print('Hello world') File " Python Forum View Course » View Exercise 1382 points Submitted by 杰里米 over 4 years ago 3.4 Bad for loop or is it python? var num = range(1,6) for x in num: if num == 3: print "fizz" elif num == 5: print "buzz" else: print num output: Traceback (most recent call last): File "codecademy", line 1 var num = range(1,6) ^ SyntaxError: invalid syntax Oops, try again. I am using chrome 3 votes permalink The var syntax is used in Javascript, not it Python. You simply define a variable by its name, like num = range(1, 6) in this case. My code is: for x in range(1, 6): if x == 3: print "fizz" elif x == 5: print "buzz" else: print x Hope I could help! :) 3277 points Submitted by Raph over 4 years ago 1 Comment telescope over 3 years ago Thanks! You definitely helped! 0 votes permalink num = range(1, 6) for x in num: if num[x-1] == 3: print "fizz" elif num[x-1] == 5: print "buzz" else: print num[x-1] Wow my code was really bad. Thanks for the help. 1382 points Submitted by 杰里米 over 4 years ago 0 votes permalink No problem! ;) Glad I could help. 3277 points Submitted by Raph over 4 years ago 0 votes permalink answer from raffael did not work for me on firefox windows 8 even though looks like it should, might have to try a different browser conbination. It does not work in the scratchpad either, but does work in labs. 2118 points Submitted by Scott Gillett over 4 years ago 0 votes permalink Yes, you might want to try using a different browser. 3277 points Submitted by Raph over 4 years ago 0 votes permalink The instructional text says the following: Write the numbers 1 to 5 using a for loop, only when the number is 3, then print fizz also, and if the number would be 5, then print buzz also. The instructor uses the term also when saying what to print, which leads one to believe the correct code should be: for x in range (1,6): if x == 3: print x,'fizz' elif x == 5: print x,'buzz' else: print x But that fails, of course. 1434 points Submitted by Brian Whitton over 4 years ago 1 Comment Jason about 4 years ago Have you check with the indentation of your code? As above code, youPython Invalid Syntax Range
Python Print Exception