Create an odd number list - python

i want to make an odd number list by using 'while' and 'for'
a = []
x = 1
while x < 101:
if x % 2 != 0:
a.append(x)
x = x + 1
print(a)
but nothing happened... and other unrelated codes which is in another sentence are also not executed.
what is my problem?

You should increase the value of x in each iteration and not only if the value is an odd number:
a = []
x = 1
while x < 101:
if x % 2 != 0:
a.append(x)
x += 1
print(a)
Though this is probably for learning purposes note that you could achieve this using the range function as follows: list(range(1,101, 2)).

When you increment x, it should be out of 'if' condition. increment should happen under while
a = list()
x = 1
while x <101:
if x%2 != 0:
a.append(x)
x += 1
print(a)

You can use the range function as well (for for loop) which handles the increment part in the loop as below:
FOR LOOP:
odd=[]
for i in range(101):
if (i%2!=0):
odd.append(i)
print (odd)
WHILE LOOP:
odd=[]
i = 1
while i<101:
if i%2 != 0:
odd.append(i)
i+=1
print (odd)

odd = [i for i in range(101) if i%2 != 0]
print(odd)

Related

Python: print even numbers including 0

Given a program that outputs all the numbers from 0 to 10, I must print only the even numbers. I'm using this code:
x = 0
while x <= 10:
x += 1
if x % 2 == 0:
print(x)
The output shows numbers from 2 to 10, excluding 0, which I need to include.
How can I print even numbers from 0 to 10?
Thanks!
** Note: I'm a beginner, so any explanation is extremely helpful.
x = 0
while x <= 10:
if x % 2 == 0:
print(x)
x += 1 # increment x AFTER the if statement, otherwise you never check if 0%2 == 0
You could also use a for-loop to simplify things further:
for x in range(11):
if x%2 == 0:
print(x)
As others have said, the problem is that you're incrementing x before you check if it's even, so you increment from 0 to 1 before printing.
But there's no need for the test, you can just loop over the even numbers:
for x in range(0, 11, 2):
print(x)
The third argument to range() is the steps, and stepping by 2 just returns even numbers.
Increment x after the if block.
hmmm....can you ask quiz questions on here? anyhow, I think the small edit to the order of operations in your loop should do it?
x = 0
while x <= 10:
if x % 2 == 0:
print(x)
x += 1
This can be esaily done with the for loop:
for i in range(11):
if i % 2 == 0:
print(i)
and that's all it takes, 3 lines :)
Its fine!
x = 0
while x <= 10:
if x % 2 == 0:
print(x)
x += 1
A slightly different slant on a on-liner ... just for fun.
print(*(i for i in range(0, 11, 2)))
>>> 0 2 4 6 8 10

I have writen this code but I don't understand why it works like this

I have a code, It gets a number as n for the amount of number you want to give, then I have another input which is the numbers we want to give the code, I want my code to find the numbers lower than 3 and produce the output but it works pretty weirdly! so help me people, Thx a lot in advance
y = []
n = int(input())
for i in range(1,n+1):
x = input()
x = x.split()
for j in x:
j = int(j)
if j < 3:
for j in x:
y.append(j)
break
print(y)
my input is like :
5
1 2 3
my output should be :
1 2
but it is like :
['1','1']
You can try this code:
y = []
n = 5
x = "5 1 2 3 2"
x = x.split()
for j in x: # for each value in x
j = int(j)
if j < 3: # if < 3, append to list
y.append(j)
print(y)
Now you can implement your function for obtaining the numbers and improve return.
You have mix with the input.
Either you receive it as one input and then you need the split or you receive is one after another (n times).
In the first option you need to split the list and iterate over it and append, in the second option you just need to check each number and append it
In the list case you don't need the n in the beginning
y = []
x = input()
x = x.split()
for j in x:
j = int(j)
if j < 3:
y.append(j)
print(y)

Unable to print in both ascending and descending range in for loop

I'm trying to create an if else rule in a for loop, but currently the values only print if the start value is less than the end value. I want the range to print in descending order if the start value is greater than the end value.
I've tried a few different options, but ideally I'm trying to create the code using only two for loops.
for i in range(x,y+1):
if (x<=y):
print(i)
else:
for i in range(y,x,-1):
print(i)
count_odd = 0
count_even = 0
for n in range(x,y+1):
if n%2==0:
count_even = count_even+1
else:
count_odd = count_odd+1
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)
Put a condition in your code if x<y and use the range accordingly either ascending or descending order:
x = 10
y = 2
count_odd = 0
count_even = 0
if x < y:
for n in range(x,y+1):
if n%2==0:
count_even = count_even+1
else:
count_odd = count_odd+1
else:
for n in range(x, y-1, -1):
if n%2==0:
count_even = count_even+1
else:
count_odd = count_odd+1
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)
Output:
Number of even numbers : 5
Number of odd numbers : 4
Edit
And your intial for loop should be:
if (x<=y):
for i in range(x,y+1):
print(i)
else:
for i in range(x,y-1,-1):
print(i)
You need to handle them differently and call the particular function once the condition hits. Here's an example.
x = 1
y = 10
def descOrder(x,y):
count_even = 0
count_odd = 0
for i in range(x, y-1, -1):
if i % 2 == 0:
count_even = count_even + 1
else:
count_odd += 1
print(i)
print ("Even:",count_even, "Odd:",count_odd)
def asceOrder(x, y):
count_even = 0
count_odd = 0
for i in range(x ,y+1):
if i % 2 == 0:
count_even += 1
else:
count_odd += 1
print(i)
print ("Even:",count_even, "Odd:",count_odd)
if x > y:
descOrder(x, y)
else:
asceOrder(x, y)

How to use a variable to find another variable in a list - python

(Python 3.x)
z=[]
x=0
while 1==1:
x=x+1
y=1
z.append(x)
while y==1:
a = 0
b = 0
if z(a)==x:
print(x)
y = 2
elif x%z(a)!= 0:
a = a+1
elif b == 2:
y = 2
else:
b = b+1
So, I made a code to find all the prime numbers until python crashes. However, it relies on z(a) for it to work. The idea is that as "a" changes, it moves on in the list.
"z(a)" is where the error lies, so does anyone know a way to fix this?
z is a list. You can approach values inside it by indexes using the z[a] operator (and not z(a) which assumes a function call with a as parameter).
I've taken the liberty of using boolean variables, += operators and unpacking values:
z = []
x = 0
while True:
x += 1
y = True
z.append(x)
while y:
a, b = 0, 0
if z[a] == x:
print(x)
y = False
elif x % z[a]: a += 1
elif b == 2: y = False
else: b += 1
I believe that's what you want to achieve (infinitely incrementing prime generator):
def prime (n): return not any([n % i == 0 for i in range(2, n)])
x = 0
while True:
if prime(x): print(x)
x += 1

How can I avoid the list error function in my code as I alter a list I am iterating?

I am looking to use pop and remove functions to remove numbers from the list 2 to 100 in order to get a list of prime numbers. The main problem is that k always ends up causing an error. Also, when a put a print function after k, it shows up only even numbers, not sure why that is happening.
x=[]
for i in range(2,100):
x.append(i)
primes=[]
count=0
while count < 99:
k = x[count]
print(k)
primes.append(k)
"""for j in range(2,100):
if k % j ==0:
x.remove(j)"""
x.pop(count)
count = count + 1
print(x)
Your out of range:
x len is 98, and the while loop counts for 48 times....
You can fix it easily like that (Just fixed the While condition to count < 48):
x=[]
for i in range(2,100):
x.append(i)
primes=[]
print len(x)
count=0
while count < 48:
k = x[count]
print(k)
primes.append(k)
"""for j in range(2,100):
if k % j ==0:
x.remove(j)"""
x.pop(count)
count = count + 1
print(x)
Errors can happen because you are removing elements from a list while iterating over it.
Consider a list [x, y, z] and you're at position 0. If you decide to remove the element at position 0 then Python will move on to check position 1 in the next iteration of the loop. But position then refers to element z (because position 1 in the list [y, z] is z, not y).
You aren't testing for prime numbers at all. You're getting only even numbers cause you're removing all odd ones. From the logic point there's no need to pre-polulate the list to further remove unwanted elements.
Here's what you're looking for:
def is_prime(n):
if n < 2 or (n % 2) == 0:
return n == 2
f = 3
while (f * f) <= n:
if (n % f) == 0:
return False
f += 2
return True
primes = [n for n in range(2, 100) if is_prime(n)]
print(primes)

Categories

Resources