Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
what I'm trying to do is to print the numbers 2-100 and mark the prime numbers with -
So, this should be the output:
2-
3-
4
5-
6
7-
8
9
10
.
.
.
100
Try this code:
primes = []
for candidate in range(2, 101):
can_be_prime = True
for prime in primes:
if candidate % prime == 0:
can_be_prime = False
break
if can_be_prime:
primes.append(candidate)
print "%d-" % candidate
else:
print candidate
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
Fix this:
for n in range(1,8):
print(n)
if(n>=2 & n<=5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is "+n)
Use and.
and is a Logical AND that returns True if both the operands are true whereas & is a bitwise operator in Python
for n in range(1,8):
print(n)
if(n >= 2 and n <= 5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is ", + n)
1. & is bitwise and operator while and is used for logical and operator.
2. You need to convert integer to string for concatenation.
Corrected code:
for n in range(1,8):
print(n)
if(n>=2 and n<=5):
print("This Number is either 2,3,4 or 5\n")
else:
print("This Number is "+str(n))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
print(i)
if i % 3 == 0:
print("cot")
elif i%5 == 0:
print("cot")
the code will output number from 1 to 100 and if the number between 1 and 100 is % by 3 or 5 it will be replace with cot,but if the number is cot the number will be put again,without the :cot
If i understand you correctly, what you are trying to do is to change the number to cot if it's divisible by 3 or 5.
So if any of those restrictions is accomplish, you need to assign 'cot' to the variable.
Like this:
i = <some number>
if i%3 == 0:
i = "cot"
elif i%5 == 0:
i = "cot"
print(i)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
For example, if the number 34560 is entered, then it has 3 even numbers (4, 6 and 0) and 2 odd numbers (3 and 5).
for num in int(len(number)):
if num % 2 != 0:
print(num, end=" ")```
you can use for example like this:
number = 34560
even = [int(x) for x in str(number) if int(x)%2 == 0]
odd = [int(x) for x in str(number) if int(x)%2 != 0]
print(even)
print(odd)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Create a function that takes a number num and returns each place value in the number.
Eg num_split(-434) ➞ [-400, -30, -4], num_split[59]->[50,9]
Try this:
def num_split(num):
num = str(num)
num = [c for c in num]
num = num[::-1]
num_lis = []
f = 1
for i in range(len(num)):
num_lis.append(int(num[i])*f)
f *= 10
return num_lis
print(num_split(59))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Let's assume:
for a in range(10):
if a == 2 or a == 5:
print (how often this condition was True)
of course will be two, in my code i want to know when my condition be True , thanks
count = 0 # set a variable for counting
for a in range(10):
if a == 2 or a == 5: # for each entry that meets the condition
count += 1 # add 1 to count
print(count) # 2