INVALID SYNTAX ERROR for 'else' statement in python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to write a quicksort program in python, however I'm getting an invalid syntax error at else statement in the second last line below:
import random
n=int(raw_input("Enter the size of the list: ")) # size of the list
intlist = [0]*n
for num in range(n):
intlist[num]=random.randint(0,10*n)
pivot=random.choice(intlist)
list_1=[] # list of elements smaller than pivot
list_2=[] # list of elements greater than pivot
for num in range(n):
if num<=pivot:
list_1.append(num)
else
list_2.append(num)
This is not a complete program as I am still writing.

add a colon after the else so that it looks like else:. and pick up a good tutorial ;)

Looks like you need a ':' after "else".

Related

Syntax error at the end of a valid for loop: for _ in range(lines): [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to run the following code:
lines = int(input("How many lines of code would you like?")
for _ in range(lines):
#do stuff
But for some reason, I'm getting a syntax error on the colon in the for loop? I've tried changing the '_' for an 'i', and putting different code at #do stuff, but nothing's working.
You are missing a parenthesis at the end of the first line
lines = int(input("How many lines of code would you like?"))

Apparently, the ":" on my if statement is invalid syntax [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am currently trying to create team selection code for my python video game.
Unfortunately, it keeps highliting the ":" of my if statement and saying that it is invalid syntax, even if i change the if statement for another. I tried everything, but after all, it IS an if statement, and i can't do much.
Heres my minimal recreation of the problem. The structure is important as there is netwroking code there;
team1=[]
team2=[]
if (len(team1)+len(team2)):
if team1==team2:
rand = (random.choice([team1, team2])
if rand == "team1":
team1.append(username)
else:
team2.append(username)
else:
if team1>=team2:
team1.append(username)
else:
team2.append(username)
else:
team1.append(username)
The problem is the stray '(' you have before random.choice([team1, team2]). Delete it so it becomes:
rand = random.choice([team1, team2])

Invalid syntax in a variable in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
The code is below
i=0
a=0
Matrix[0]=0
Matrix = [1 for i in range(1000)]
while i<1000:
i=i+1
Matrix[i]=(1/1000)*((i*(1/1000))^2
a=a+Matrix[i]
print (a)
When I try to run the program, Python highlight the first a in the 8th row and it says invalid syntax.
I can't figure out what is the problem with the code.
Much appreciated if you helped
Corrected Code:
i=0
a=0
Matrix=[1 for i in range(1000)]
while i<1000:
Matrix[i]=(1/1000)*((i*(1/1000)))**2
a=a+Matrix[i]
i=i+1
print (a)

Python in proccess of geting to pig game [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Issue occurred right after I added the and x= "J1234": I don't know what's going on.
original = ""
if len(original) > 0 and x = "J123":
raw_input("Enter a word:")
original = raw_input('word')
print "original"
else:
print "empty"
x.isalpha()
So, you're getting a SyntaxError. To fix this, change:
x = "J123":
to...
x == "J123":
In Python the former is for setting values and the latter is for checking them. Therefore when you use this the wrong way round the syntax is incorrect.

most efficient code for printing odd numbers from 1-99 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
The task is to print the odd numbers from 1-99 on separate lines.
Codeeval deemed this code partially correct (98 out of 100): (edited)
liszt = (i for i in range(1,100) if i%2!=0)
for i in liszt:
print i
Codeeval deemed the below code completely correct:
liszt = range(1,100)
for i in liszt:
if i%2!=0:
print i
New to Python, so just looking to understand why one method might be considered better than the other. Is the second method more efficient?
Thanks for the help!
In the first code you are iterating over two generators first range(1, 100) and then over liszt whereas in the second case the iteration is only over liszt. Other than that the operation is same in both the cases, so the second method is more efficient.
Since every second number after 1 would be odd, a better solution could be:
for i in range(1, 100, 2):
print(i)
print("\n".join(str(i) for i in range(1,100,2)))

Categories

Resources