'How to take the result from if condition out of it?
I would like to ask something about python logic and how could i solve this problem. Here is my code:
controlRatio = 1
for i in range(10):
if controlRatio == 1:
if (i%2==1):
print(i)
elif controlRatio == 2:
print(i)
#code#
#Because my code long is and i don't want to write the code below if and also below elif.
#I would like to take the result from the condition and use it below the 'for'.
In my case this would be either 13579 or 0123456789
How could i take the 'i' result out of the if condition and use it. I tried with a variable, but it doesn't work as i expected
Solution 1:[1]
declare the value of i to another variable in the if statement of your choice. then you can use the variable. in this example "tmp". you could also save all the values for i in a list and add every new element with <list_name>.append(i)
tmp = 0 #the variable
controlRatio = 1
for i in range(10):
if controlRatio == 1:
if (i%2==1):
print(i)
tmp = i #the variable
elif controlRatio == 2:
print(i)
ยดยดยด
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
