How to set new value for loop in Python?

For example, I want to make a loop where it will go counter from 1 to 999 and each vyvedenie value then it should be divided into 2 different operand and is displayed next to the figure on which to share. Thank you!
April 4th 20 at 13:25
1 answer
April 4th 20 at 13:27
Solution
It?
for num in range(1, 9+1):
 print("num: {}, num/2: {}, num/3: {}".format(num, num/2, num/3))


num: 1, num/2: 0.5, num/3: 0.3333333333333333
num: 2, num/2: 1.0, num/3: 0.6666666666666666
num: 3, num/2: 1.5, num/3: 1.0
num: 4, num/2: 2.0, num/3: 1.3333333333333333
num: 5, num/2: 2.5, num/3: 1.6666666666666667
num: 6, num/2: 3.0, num/3: 2.0
num: 7, num/2: 3.5, num/3: 2.3333333333333335
num: 8, num/2: 4.0, num/3: 2.6666666666666665
num: 9, num/2: 4.5, num/3: 3.0
How to allocate in this decision, only positive integers ? - Kelsi_Rath61 commented on April 4th 20 at 13:30
You mean whole?
for num in range(1, 9+1):
 if num % 2 == 0:
 print("num: {}, num/2: {}".format(num, num/2))
- Wiley_Kuhn39 commented on April 4th 20 at 13:33
@Wiley_Kuhn39,
for num in range(1, 999+1):
 if num % 5 == 0 and num % 7 == 0:
 print("num: {}, num/5: {}, num: {}, num/7: {}".format(num, num/5, num, num / 7))


Correct to write so? - Kelsi_Rath61 commented on April 4th 20 at 13:36
@Kelsi_Rath61, Yeah, so.. About the division by 5 and 7 above is not seen. Now You get the output string only if the number is divisible by 5 And by 7 without remainder. If you need to display only when that is NOT divisible without a remainder and at 5 And 7, then change the condition to
if not num % 5 == 0 and not num % 7 == 0:
or is it the same
if num % 5 != 0 and num % 7 != 0:

If you want to calculate the result, then:
a = 0
b = 0
for num in range(1, 999+1):
 // the number is divisible by 5 and 7
 if num % 5 == 0 and num % 7 == 0: 
 print("num: {}, num/5: {}, num: {}, num/7: {}".format(num, num/5, num, num / 7))
 a += 1
 // number is NOT evenly divisible by 5 and 7
else:
 b += 1
print("A: ", a)
print("B: ", b)
- Wiley_Kuhn39 commented on April 4th 20 at 13:39
@Kelsi_Rath61, it is better to format write format(num, num//5, num//7) to output integers without zero decimals. - Marjory51 commented on April 4th 20 at 13:42
@Wiley_Kuhn39, Thank You! - Kelsi_Rath61 commented on April 4th 20 at 13:45
@Wiley_Kuhn39Made! ) - Kelsi_Rath61 commented on April 4th 20 at 13:48

Find more questions by tags Python