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
Related
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 last month.
Improve this question
this is my code currently:
previous = 0
count = 0
counts = []
item = str(data)
for x in range(len(item)):
if dates[x][3] != previous[3] or dates[x][4] != previous[4]:
if negative[x] != "No Negative":
counts.append(count)
count = 0
count += 1
previous = dates[x]
print(counts)
I keep getting an "'int' object is not subscriptable error. my values for dates and data are lists and I'm assuming the issue is that they aren't strings but nothing I do fixes the error.
thank you
The problem is in previous[3].
previous is int, it's not subscriptable.
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 ()
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
How can I change python variable value with if.
so like we have a=5, and there we have an input b, if b =5, i want the program to change a to 3.
This is the program I tried to make but it didnt work, hope you have solution.
a=5
b=int(input())
if b==5:
a==3
print(a)
b = int(input())
if b == 5:
a = 3
print(a)
If you want to set a new variable only use one equal sign not two.
you need to change the 4th line from a==3 to a=3when you use double '=' you are comparing and the result is boolean (true or false) and when you use just a single '=' your are assigning a value to something.
For example:
a = 3==4 print(a)
This will output FALSE, since 3 doesn't equal 4 and that value was assigned to variable a
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'm taking a course through Stackskills on python and I'm on iterators and loops. The teacher is getting a different outcome than I am and I was wondering why:
You have:
sum = sum + sum # you are only adding sum to itself
you should change it to:
sum = sum + num # here you will be adding the current list item to sum
In the loop your teacher is adding
sum = sum + num # Attention its an n
But you are adding:
sum = sum + sum # which is sum = 0 + 0
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
I having some problems trying to understand generators.
Do these function's execution time differ when doing the same task?
def slow_sum(size):
x= 0
for i in range(size):
for j in range(size):
x += i + j
return x
def fast_sum(size):
return sum( [ (i+j) for j in range(size) for i in range(size)] )
size = 2000
slow_val = slow_sum(size)
fast_val = fast_sum(size)
assert slow_val == fast_val, "Values are not equal"
When profiling both functions on my computer using cProfile I got these result, but I expected them to be similar.
Total Time
slow_sum(2000)
0.85 ms
fast_sum(2000)
0.05 ms
Original File: https://pastebin.com/fDfaSqyZ
My Output: https://pastebin.com/wyy3v3iy
You're looking at the wrong column of the profiler output. tottime doesn't count all the time fast_sum spends inside the sum call or the list comprehension's stack frame. You should be looking at cumtime, which is near equal for the two functions.