Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I know the similar question has been asked before and I looked through a lot of previous questions but couldn't find the answer hence I am posting it.
I have a basic question about for loop. I have been using VBA for a long time and recently have to learn Python for work. I will need to use FOR LOOP in the middle of the code and looks like there in no NEXT following the FOR in python. For example, this is what the code looks like in VBA:
sub test()
dim i as integer
dim j as integer
for I = 1 to 10
j = j+i
Next
.
Print(j)
.
.
'rest of the code
end
Can someone please tell me how to define the FOR LOOP in the middle of the code? Thanks
Python alternative for your function
def test():
for i in range(1, 10):
print(i)
Note out that python has no variable declaration as it is not strongly-typed language.
Also instead of next it has indentation as depth of execution.
In python for loop are used over iterable object or generator.
List:
num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in num:
print x
Generator:
for x in range(10):
print x
For more example, see https://wiki.python.org/moin/ForLoop
Your question not very clear, so if this does not answer your question do not hesitate to edit your message.
This will also do your job.
for i in range(10): # from 0 to 9.
j = j + i
Related
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 days ago.
Improve this question
This is a program that multiplies each base value nums times
def calNums(base, *nums):
for i in range len(nums):
total = base
for j in nums-1:
total *= base
print(f'{}의 {} 제곱 값은 {}이다'.format(base, nums, total))
calNums(5, 1, 2, 3)
calNums(2, 2, 4, 6, 8, 10)
I have a problem with 2nd line.
The problem here is range is not a keyword, it requires you to use parentheses.
Line 5:
for i in range(len(nums)):
And I have absolutely no idea what you're trying to do here.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
Video Puzzle
Current code:
for i in range(6,0,-2):
Spaceship.step(2)
Dev.step(i)
for idk in range(3):
Dev.turnRight()
Dev.step(i*2)
Dev.turnRight()
Dev.step(i)
In this puzzle the objective is to get all the item (blue thing). With 6 line of code, and I'm currently at 8 line of code. I don't know how to minimalize the line of code.
Note:
Dev.step() is the robot, it can go backward by set the value by negative.
Spaceship.step() is the spaceship, it can not go backward.
You can avoid pythonesque code like so:
for i in range(6,0,-2):
Spaceship.step(2)
for idk in range(4):
Dev.step(i)
Dev.turnRight()
Dev.step(i)
This is a possible solution in only 6 lines:
for i in range(6, 0, -2):
Spaceship.step(2)
for k, j in enumerate([1, 2, 2, 2, 1]):
Dev.step(i * j)
if k != 4:
Dev.turnRight()
The idea is to group all steps of the robot in a list in order to do a nested loop and turn only if it is not the last element of the list.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have run the the following functions and studied them line-by-line. I understand how the outer for-loop in f(n) works and how the while-loop works in g(n) but I understand the role of the inner for-loop in f(n). Also, how do these loops work with t = t+1? Thanks in advance!
def f(n):
t=0
for i in range(n):
for j in range(2*i):
t=t+1
return t
f(5)
def g(n):
t=0
j=n
while j>1:
t = t+1
j = j/2
return t
g(32)
The inner loop keeps adding 1 to t until it adds 2 times each item from the outer loop. So it adds 0 + 2 + 4 + 6 + 8. The range(5) is similar to a list equivalent to [0,1,2,3,4].
t=t+1 simply adds 1 to the value of t every time the line is run.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In such a loop, if the variable no is equal to 8, how can I return the 8th loop again? I couldn't do it, I'm confused.
no -= 1 command not working
for no in range(0,100):
print("No:",no)
if no == 8:
no -= 1
print("8 done return again")
Modifying the variable no doesn't do what you want here, because Python's for no in range(...): loop gets the values for no from the range iterator. That iterator doesn't know about the variable no, and will keep yielding the values from the range regardless of what you do to no.
One way to solve your problem is to use a while loop and explicitly control the loop variable no yourself. A more "Pythonic" way is to use itertools to construct an infinite iterator that yields the numbers
0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8...
For example:
from itertools import chain, repeat
for no in chain(range(8), repeat(8)):
print("No:", no)
Keep in mind that you will still need some way out of the infinite loop.
If you want an infinite loop when you reach 8, then you can use this :
no = 0
while no < 100:
print("No:",no)
if no == 8:
print("8 done return again")
else:
no += 1
This should infinitely repeat "8 done return again". By switching the if statement to a while one.
for no in range(0,100):
print("No:",no)
while (no == 8):
print("8 done return again")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I find the smallest odd number in a list of integers, if the problem statement says that I can't use for and if?
You can use filter and min:
s = [4, 2, 3, 4, 7, 1]
smallest_odd = min(filter(lambda x:x%2 != 0, s))
Output:
1
Why should anyone consider using for or if?
min(numbers, key=lambda n: (n&1==0, n))
you can use
ints = [2,6,8,4,6 ]
srt = sorted(ints)
while len(srt) > 0 and not srt[0] % 2:
srt = srt[1:]
s = None
while len(srt):
s = srt[0]
srt=[]
print(s)
to circumvent the rules. You sort the list, discard any even values from front.
The resulting list is either empty or has the value at front.
The second while is only entered if the list contains elements, and the list is reset to empty (which is false and will not renter the while).
It will print "None" or the lowest not even number.