Python: print even numbers including 0 - python

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

Related

Create an odd number list

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)

How do you mulitply via addition?

I'm having trouble trying to
compute x * y using only addition, printing the intermediate result for each loop iteration, and
printing the final number. This is my
teacher's example of what the output should look like
I could really use some help figuring it out,, I don't really understand how to yet.
Edit: I am a first time coder so I'm not very skilled, I do know the basics though (Also very new to this website) Here's what my code looks like so far:
x = int(input("Input positive x: "))
y = int(input("Input positive y: "))
z = 0
w = 0
if x < 0:
exit("Please input a positive number for x")
if y < 0:
exit("Please input a positive number for y")
def multiplier(number, iterations):
for i in range(1, iterations):
number = number + 3
print(number) # 12
multiplier(number=3, iterations=4)
My answer. Little shorter than the others.
Ok, try thinking of multiplication as adding a number with itself by x times. Hence, 3*4 = 3+3+3+3 = 12, which is adding 3 by itself 4 times
Replicating this with a for loop:
#inputs
x = 3
y = 4
#output
iteration = 1
result = 0
for num in range(4):
result += x
print(f'Sum after iteration {iteration}: {result}')
iteration += 1 #iteration counter
The resulting output will be:
Sum after iteration 1: 3 Sum after iteration 2: 6 Sum after iteration
3: 9 Sum after iteration 4: 12
The code is simple and straightforward,Check which number is bigger and iterate according to that number.
x = 3
y = 4
out = 0
if x > y:
for v in range(0,x):
out += y
print("after iteration",v+1, ":", out)
if y > x:
for v in range(0,y):
out += x
print("after iteration",v+1, ":", out)
print(x,"*", y, "=", out)
Use print to print inside the loop to show each number after adding and the number of iteration occurs.
Here is a start. This assume both arguments are integers 0 or more:
def m(n1, n1a):
n1b = 0
while n1a > 0:
n1b += n1
n1a -= 1
return n1b
n = m(9, 8)
print(n == 72)

Python prime number generator prints out every number

I made this prime number generator but it just prints every number (1, 2, 3...).
I am aware that there are other questions about this but I don't understand any of them. Here is what I have so far:
x = 1
y = 1
while x >= y:
if x % y == 0:
print(x)
x = x + 1
y = 1
else:
y = y + 1
y = 1
From your question, I think, it would be better try this way :
n = 10
i = 2
while i < n :
prime = 1 # reset the `prime` variable before the inner loop
for a in range(2, i):
if i%a == 0:
prime = 0
break
if prime:
print (i)
i += 1
Output :
2
3
5
7
There's something called a rubber ducky test where you explain your code, step by step, to a rubber duck. It forces you to walk through every step and you'll often find the error. In this case, think about what y is when you do x%y. You'll see that y=1 the first time this is called. So it will iterate through the loop incrementing x by 1 and y will remain set it 1. And x%y when y=1 will always be...

Simple python while loop troubles

So Im just wondering why my code below is not functioning as expected,i want it to keep adding odd values to the odd count when given an odd number, likewise with the evens. but no matter what i put, it just adds it to the odd count. suggestions?
odd_value = 0
even_value = 0
x = int(input('enter'))
while x != 0:
if x == 3 or 5 or 7 or 9:
odd_value += x
elif x == 2 or 4 or 6 or 8:
even_value += x
x = int(input('enter'))
print('The sum of the odds is ',odd_value)
print('The sum of the evens is' ,even_value)
You've created an infinite loop. x will never equal to 0, and hence your loop will always keep going. It doesn't have an exit point. You can use a break statement anywhere to get out of the loop when you want.
Also, your logic is wrong:
if x in (3, 5, 7, 9):
odd_value += x
elif x in (2, 4, 6, 8):
even_value += x
or...
if x == 3 or x == 5 or x == 7 or x == 9:
odd_value += x
elif x == 2 or x == 4 or x == 6 or x == 8:
even_value += x
Now it evaluates each individual statement properly, instead of just seeing something that is implicitly True.
Here's why your original statements don't work:
>>> if x == 3 or 5 or 7 or 9:
... print "Oh Darn!"
Oh Darn!
Let's see this in english:
if x is equal to 3 # Nope!
if 5 # That's True so let's go on!
Execute the if code block
Forget the elif code block

Generating Perfect Squares python

I am trying to write the simplest code possible that will continuously print out Perfect Squares. My code so far reads this
x = 1
y = 1
while True:
final = x * y
print final
x = x + 1
y = y + 1
When I run it, I get a syntax error. The red bit is on the "final" in "print final"
Apologies if it is a really basic error, but I'm stumped.
Thanks for taking the time to read this
I assume you're using Python 3. print is now a function in Python 3.
Do print(final) instead of print final.
Also, it seems like your x and y values are holding the same thing. Why not discard one of them and use just one?
x = 1
while True:
final = x * x
print(final)
x = x + 1
Or better yet, use the builtin exponentiation operator **.
x = 1
while True:
final = x **2
print(final)
x += 1
Also, your code seems to be going into an infinite loop. You may need a condition to break it.
For example, if you want to break when x reaches 10, just add a condition in the while loop, like follows:
x = 1
while True:
final = x **2
print(final)
x += 1
if x == 10:
break
OR Specify a condition in the whilestatement, like follows:
x = 1
while x < 10:
final = x **2
print(final)
x += 1
OR Use a for loop.
for i in range(10):
print(i**2)
In Python 3, print is no longer a statement but a function, so you must enclose what you want to print in parentheses:
print(final)
Here's a link about the function.
You also have an IndentationError, y = y + 1 should be given a space.
And you can simplify that to y += 1 (which is the same thing, in regards to integers)
You can also add a condition to the while-loop:
x = 0
while x < 5:
print x ** 2
x += 1
Prints:
0
1
4
9
16
I would reccomend using Python 3 if you're not already. Also, as x and y are the same value at all times you only need one of them. So instead of reading:
x = 1
y = 1
while True:
final = x * y
print final
x = x + 1
y = y + 1
You should write:
x = 1
while True:
final = x * x
print(final)
x = x + 1
Hope this helped!
Jake.

Categories

Resources