from math import *
def p():
for a in range (2, 100): ##for a in range 2 to 100
for y in range (2, a): ##for y in range 2, 100
x = 2*a+1 ##x has the following value
myprimelist = [] ##creating my empty list
if x > y:
while (x % y != 0):
myprimelist.append(x) ##append to list and continue with the same y until modulo is zero after which it reaches to else
y += 1
print (x,'nu se imparte egal la',y)
return [y] ##change the y
else:
y += 1
return [y]
else:
a += 1
return[a]
a += 1
return[a]
print (myprimelist);
I'm trying to make a function that will tell me all prime numbers from 2 to 100 but for some reason the loop stops and I don'know why. Please help. Thank you!
return means end of function. Everytime it is encountered the function stops executing.
It has nothing to do with python 3, its about return statement. You can try the following code as it checks only from 1 to sqrt(n), as it makes no point trying to check for numbers greater than sqrt(n). Hope this helps.
import math
for num in range(1,101):
if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
print num
Related
This is what I have done:
Can anybody tell me where I went wrong?
num = 0
x = 1
while x <= 500:
num += x
x += 5
print(num)
Your indentation is also wrong. It needs to be like this
num = 0
x = 0
while x <= 500:
num += x
x += 5
print(num)
But, if you want to print only the final sum, you can use the print statement outside loop
num = 0
x = 0
while x <= 500:
num += x
x += 5
print(num)
Logic: We have to use while loop to check every number. We have to check whether the number is divisible by 5 or not, for that, we have to put if condition and keep on adding +1 to go from 0 to 500. If the condition is satisfied, add to to a variable whose initial value is 0. You code:
x=0
r=0
while x<=500:
if x%5==0:
r+=x
x+=1
print(r)
Or you can do it this way, start the initial value from 0, updating value of x should be 5, add it to variable whose initial value is 0. You alternative code:
x=0
r=0
while x<=500:
r+=x
x+=5
print(r)
The second code will help you rectify your personal code. Have a look
Yet another way:
x = 0
acc = 0
while (x := x + 5) <= 500:
acc += x
Here we use an assignment operator inside while() to get the value while incrementing it at the same time.
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'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)
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...
I am trying to make a simple XOR program. Once I finished checking my syntax, I ran my program and ran into an infinite loop. I can't find my error. Help?
def disencode(n):
seconde = raw_input("Input_Second_String")
y = len(n)
x = 0
while x < y:
if n[x] == seconde[x]:
print 0
else:
print 1
x =+1
disencode(raw_input("Input_First_String"))
x=+1 should be x += 1, as with your current code, you never increment x,
as x =+ 1 is the same thing as x = 1.
You're effectively setting x as 1, never increasing it, and asking the loop to run while x < y, which is infinite.
See here for more information
use x += 1 for incrementing x instead of x =+1