Curl inside while loops 1 - python-3.x

what is the difference between this code
x = 10
while x:
x = x - 1
if x % 2 == 0:
print(x, end = ' ')
and this code
x = 10
while x:
if x % 2 == 0:
x = x - 1
print(x, end = ' ')
I'v just start the python lerning thanks in advance

In the first code block:
You subtract 1 from x after every loop iteration, thus the value of x will be:
10 (outside the loop)
9
8
...
1
0
It will print x whenever x is even
8 6 4 2 0
The second code block will loop infinitely after the first iteration.
The inside of the if statement is executed only one when x = 10. After that, the value of x is always 9 and it stays like that infinitely.

Related

Could someone explain, how the output of the below snippet code is 100?

Here is the code:
x = 0
while (x < 100):
x+=2
print(x)
The value of the x is incremented by 2 in every step. The sequence of the x value is 0 2 4 .. 98. When the value of x becomes 100 (100<100) this is false. Here the while loop terminated and hence the 100 is printed.
x = 0
while (x < 100):
x+=2
print(x)

Adding together elements contained in two lists

I am trying to add elements of two lists together e.g. listing all integers below 10 that are multiples of 3 or 5. The sum of these multiples should be 23 (3+5+6+9) but I keep getting 18. I have only just starting programming and learning python. Here's my code:
for i in range (1,10):
if i % 3 == 0:
print(i)
for x in range (1,10):
if x % 5 ==0:
print(x)
sum_multiples=i+x
print(sum_multiples)
What you are trying to do:
3 + 6 + 9 + 5 = 23
What you are doing:
9 + 9 = 18
This is what you want:
l1 = [x for x in range(1, 10) if x % 3 == 0]
l2 = [x for x in range(1, 10) if x % 5 == 0]
print(sum(l1) + sum(l2))
First off, let me address your problem. When you add variable i and variable x, they are both value of 9. This is because after the loop their value is 9, and because you are adding the values after the loop, you are adding 9 + 9.
You can fix this by making a variable outside the loop and adding the number to the variable. My code would be:
total = 0
for i in range (1,10):
if i % 3 == 0:
print(i)
total = total + i
for x in range (1,10):
if x % 5 ==0:
print(x)
total = total + x
print(total)
(this is just based off what you were doing, I would probably condense it into one loop)
You could so something like this:
sum_multiples = 0
for i in range(1, 10):
if i % 3 == 0 or i % 5 == 0:
sum_multiples += i
print(sum_multiples)
If you prefer a one line solution:
print(sum(i for i in range(1, 10) if i % 3 == 0 or i % 5 == 0))

Finding sum of n odd numbers following a given integer

Read an integer N that is the number of test cases that follows. Each test case contains two integers X and Y. Print one output line for each test case that is the sum of Y odd numbers from X including it if is the case. For example: for the input 4 5, the output must be 45, that is: 5 + 7 + 9 + 11 + 13 for the input 7 4, the output must be 40, that is: 7 + 9 + 11 + 13
Try this I was just working on it.
x = int(input("Enter Value of X: "))
y = int(input("Enter Value of Y: "))
result = 0
itt = 0
while itt != y:
if x % 2 == 0:
x += 1
else:
itt += 1
result += x
x += 1
print(result)

Prime number generator returns none at the end when inside a function sometimes

I made this prime number generator in python:
for x in range(a , b):
y = 2
while 1 == 1:
if x % y == 0:
if x == y:
print(x)
break
else:
break
y += 1
and it returns all prime numbers up to b as long as a > 2 and b > 3
for example, when a = 2 and b = 11 i get this
2
3
5
7
but when I nest it inside a function like this and print it:
def f(b,a=2):
for x in range(a , b):
y = 2
while 1 == 1:
if x % y == 0:
if x == y:
print(x)
break
else:
break
y += 1
print(f(11))
I get this:
2
3
5
7
None?
Why is it printing a none while inside a function and not without? And how would I fix this?

Python. Nested While Loop

I'm a complete beginner trying to write a nested while loop using Python where I'd like a countdown to print three times.
Currently I have:
def amigo (counter, n):
while counter > 0:
while n > 0:
print (n)
n= n - 1
print('Hola!')
counter = counter - 1
Where I'm setting counter and n to both equal 2.
What I'd like it to do is print:
3
2
1
Hola!
3
2
1
Hola!
But right now it is printing:
3
2
1
Hola!
Hola!
Can someone point me in the right direction?
always reset counters.
Before each loop set counter value to initial position
For your inner loop it will be i = n
Use assignment with actions
Operator x += y is equivalent to x = x + y
Then your code will be n -= 1 instead of n = n - 1
Improved code
def amigo (counter, n):
while counter > 0:
i = n # Reset counter befor cycle. Used i to prevent editing n's value
while i > 0:
print (i)
i -= 1 # Decrease i by 1, will loop from N to 1
print('Hola!')
counter -= 1 # Descrease counter by 1

Resources