Expected ":"Pylance [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 1 year ago.
Improve this question
I can't find where the missing ":" is.
My code is:
def A_star_Traversal(cost, heuristic, start_point, goals):
path = []
hashTable = {}
for i in goals:
hashTable[i] = 1
n = len(cost)
visited = [False for j in range (n)]
pq = [[0+heuristic[0],start_point]]
heapq.heapify(pq)
while(len(pq)):
v = heapq.heappop(pq)
if(hashTable.get(v)):
path.append(v)
break
visited[v] = True
path.append(v)
f=1
for i in range n: '''this is where i get the error'''
if(visited[i]==False and cost[v][i]>0):
pq.append([cost[v][i]+heuristic[i],i])
heapq.heapify(pq)
f=0
if(f):
path.pop()
return path
I am getting the error at for i in range n:.
I looked through all the for and while loop and if else condition statements making sure that each one had ':'. However, despite finding no missing ':', the compilation clearly indicates that I have missed it somewhere. I am hoping that it would not be the same for others.

You appear to be missing brackets around the n. In Python 3 (if that is what you are using) it should be:
for i in range(n):

the error comes because u didn't enclosed the n in brackets
for i in range(n):

Try doing it like this
for i in range(n):
#rest of the code
I think you are getting the error because you missed the parenthesis ()

Related

how can i fix the problem in for loop 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 yesterday.
Improve this question
error in for loop in python
I wanted to run this code, but this error occurs in the for section
h want to know why i cant use unequal in for
x = 18
y = 12
for n !=0:
n = x%y
print(n)
A mentioned by #Gabio, you should use a while loop like:
x = 18
y = 12
while n !=0:
n = x%y
print(n)
A for loop requires a defined range/collection of items to iterate over, whereas a while loop executes until a condition is met:
for n in range(0,10):
# do something

Dynamic programming: Number of ways of partitioning a set of numbers [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
While reading an algorithm book, I found the following exercise.
Given a set of n elements, write an algorithm that finds number of
ways of partitioning it.
Example: When n = 2, there are 2 ways of partitioning the set(into
two sets with one element, or into the original set and the empty set).
And instead of the algorithm, I tried the python code using dynamic programming.
def ways(n):
dp = [0]*(n+1),
sum = [0]*(n+1) ## declaring 2 arrays of n+1 size
dp[0] = 0
dp[1] = 1
sum[0] = 0
sum[1] = 1
lastcalc = 1 # last calculated var
for i in range (2,n):
if lastcalc < i/2 :
for j in range (lastcalc, i/2):
sum[j] = sum[j-1] + dp[j]
lastcalc = (i/2) # update the lastcalculated variable
dp[i] = sum[i/2]
return dp[n]
print(ways(2))
But, the code won't work and gave me an error.
TypeError: 'tuple' object does not support item assignment
My question: how can I fix this? Can I say this code applied a dynamic programming?
You have a comma at the end of the declaration of dp. This makes it a tuple, not a list, and tuple are not modifiable. Just remove it, it's a typo.

Python if branch “expected an indented block” [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
Sorry if i am still newbie, i get an indentation error at line 13, please help me. I already read much article from google especially stackoverflow.
list1 = []
long = False
count = 0
TVR_count = 0
for i in range(0,len(df1Lat)):
for j in range(0, len(df_ANTV)):
if (df1Lat.start_time.values[i][0:5] == df_ANTV.daypart_variable.values[j][0:5]):
if (df1Lat.end_time.values[i][0:5] == df_ANTV.daypart_variable.values[j][0:5]):
df1Lat.TVR_total = df_ANTV.TVR.values[j];
list1.append(df1Lat.iloc[i];
else:
long = True
count += 1
elif (long == True):
count += 1
TVR_count += df_ANTV.TVR.values[j]
if ((str(df1Lat.end_time.values[i])[0:5]) == (str(df_ANTV.daypart_variable.values[j])[0:5])):
long = False
df1Lat.TVR_total = TVR_count/count
list1.append(df1Lat.iloc[i])
count = 0
TVR_count=0
else:
pass
dfLat = pd.DataFrame(list1)
dfLat[['date','channel','product','start_time','end_time','TVR_total']].head(60)
You forgot a closing bracket on this line list1.append(df1Lat.iloc[i];
Replace it with this: list1.append(df1Lat.iloc[i])
Here's some points to improve your code overall:
Line10: df1Lat.TVR_total = df_ANTV.TVR.values[j] instead df1Lat.TVR_total = df_ANTV.TVR.values[j];
Line11: list1.append(df1Lat.iloc[i]) instead list1.append(df1Lat.iloc[i];
What are the comprehensive lint checkers for Python?
Follow PEP8 guidelines, it's sort of widely standard when coding python
But the most important, code should be like a good book, if I was going try to understand your little code fragment, do you think the chosen words would help me to understand its meaning? :)

Function output not printing? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
trying to see whether the functions work, I tried printing out to see whether the random list could be sorted, however nothing seems to be printed. Also how would I measure the time it takes to sort the list out?
import random
import time
def mergeSort(mylist):
if len(mylist) <= 1:
return mylist
mid = len(mylist) // 2
left = mergeSort(mylist[:mid])
right = mergeSort(mylist[mid:])
return merge(left,right)
def merge(left,right):
if not left:
return right
if not right:
return left
if left[0] < right[0]:
return [left[0]]+merge(left[1:],right)
else:
return [right[0]]+merge(left,right[1:])
newList=[]
for i in range(100):
newList.append(random.randint(1,1000))
val = mergeSort(newList)
print (val)
Nothing is outputted, why?
You blew your indentation. Here's the correct version:
def mergeSort(mylist):
if len(mylist) <= 1:
return mylist
mid = len(mylist) // 2
left = mergeSort(mylist[:mid])
right = mergeSort(mylist[mid:])
return merge(left,right)
In the version you posted, you have the main body of the routine still inside the if statement, but after the return. This means that it can never execute. If you look, you should find that your program did print something, the value None.

how to assign a two-dimensional variable to an array [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
I am trying to make a program to generate up to ten "zombies" in pygame. however, i am having trouble with the following code:
ZOMBIE = pygame.image.load('zombie.png')
zombieamount = 0
**zombiepos(1) = [-1,-1]
zombiepos(2) = [-1,-1]
zombiepos(3) = [-1,-1]
zombiepos(4) = [-1,-1]
zombiepos(5) = [-1,-1]
zombiepos(6) = [-1,-1]
zombiepos(7) = [-1,-1]
zombiepos(8) = [-1,-1]
zombiepos(9) = [-1,-1]
zombiepos(10) = [-1,-1]**
while True:
for i in range (1,10):
time.sleep(5)
x=randint(0,30)
y=randint(0,20)
**zombiepos(i) = [x,y]**
global zombieamount
zombieamount =+ 1
for i in range (1,zombieamount):
window.blit(ZOMBIE, (zombiepos(i)[0]*TILESIZE, zombiepos(i)[1]*TILESIZE))
however, the program highlights the bolded staement, stating "can't assign to function call" my teacher stated that it might be because i am trying to assign a two-dimensional variable to a one-dimensional array. Any tips?
You need to tell python your zombiepos is an array before you assign to it through indexing:
zombiepos = []
for _ in range(10):
zombiepos.append([-1,-1])
you'll have to change the for statement in the while to range(10) as well.
the global statement is unnecessary. you'd need to use it if you were accessing zombieamounts in a function.
also, as mentioned in some of the comments to your code, when you access the items within zombiepos you need to use square brackets [] not parentheses

Categories

Resources