I do not understand foor loop [closed] - python

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
Why inside for loop is written upper + 1 and not just upper. If I change upper+1 with upper and run the code, I will have the same result!
for num in range(lower,**upper + 1)**
# Python program to display all the prime numbers within an interval
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower,upper + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)

It's because range(x,y) stops at y-1. For example:
for i in range(2,5):
print(i)
will give you:
2
3
4
So if you want to check upper as well, you should add 1, so upper+1. Your code returns the same results since upper is not prime, so even if upper is checked (with upper+1 in range), upper will not be printed.

Related

pls help me to get this prime number code right [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
n = int(input('Enter number '))
def prime(n):
for i in range (2,int(n-1)):
if (n%i)==0:
print('no is not prime')
break
else:
print('no is prime')
break
prime(n)
this will display prime numbers from 900 to 1000
lower = 900
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)

Why we use count variable & and how its work? [closed]

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 2 years ago.
Improve this question
#program of prime number from 2 to 100
for i in range (2,100):
count = 0
for j in range(1, i+1):
if i % j = 0:
count = count + 1
if count == 2:
print(i)
First of all, your code seems to not work due to a syntax error(might be a typo).
if conditions in python, requires double equals (==).
if i % j == 0:
In the question you posted, the count variable acts as a flag or counter variable.
A prime number is a number which can only be perfectly divided, by 1 or the number itself. (eg: 11 can be divided by 1 and 11 only, therefore a prime number)
So the counter variable i.e. count holds the number of times that a number(2-200) gets perfectly divided(i.e. (%) modulo operation results in value 0). If the value of count is 2 then we can confirm that it is a prime number since a prime number can only be divided with 1 and the number itself (hence count=2). And if the count == 2 after all the possible divisions happening in the j loop (2nd for loop), we can conclude that it is a prime number.
for i in range (2,200):
count = 0
for j in range(1, i+1):
if i % j == 0:
count += 1
if count == 2:
print(i)
count variable is being used to ensure if the particular number i is being divisible by any other number other than 1. Because if it does, then it is not a prime number. The moment it becomes 2, it means that number is divisible by 1 and some other number less than i proving it be a non-prime number.
Run this to get primes from 100, you can change the 100 to whatever you want
for num in range(100):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")

Count simple numbers in diapasone python [closed]

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 2 years ago.
Improve this question
Task is : input two numbers that is a diapasone . Then count a quantity of simple numbers into this diapasone.
Please correct my python code below
import math
count = (int, input().split())
for i in count:
n = int(input())
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
print(count)
If you want to count the prime numbers in the closed interval [j, k] (which is given with input()), then you could use
import math
interval = tuple(map(int, input().split()))
count = 0
for n in range(max(2, interval[0]), interval[1]+1):
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
else:
count += 1
print(count)
Explanation:
use map to apply int on each string
use count as a counter variable
use range as an iterator over integers in given interval
start prime number tests with 2, even if lower bound is smaller
count up only if no divisor is found (else clause of for loop)

How to exclude a number from a range in python? [closed]

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 3 years ago.
Improve this question
I'm new to coding, and I wrote a program using a for loop and a range to check if a number from that range is odd or even. However I want to exclude the number 0
for num in range (0, 11):
if num %2 == 0:
print (num,' is an even number!')
else:
print (num,' is an odd number!')
I expected the output of all the numbers but not the 0.
could someone help me?
As answers have already mentioned, range(1, 11) will produce 1 2 3 4 5 6 7 8 9 10 as requested. However you can exclude specific numbers by doing:
all_numbers = range(0, 11)
exclude_set = {0} # a set containing the value 0
numbers = (num for num in all_numbers if num not in exclude_set)
This could be useful if you have a set of numbers that you know are wrong, but otherwise have a contiguous range.
"""Represent retail stores in a chain"""
store_numbers = range(1, 51)
# stores 14, 17, and 32 have been closed, so
closed_stores = {14, 17, 32}
valid_stores = (store for store in store_numbers if store not in closed_stores)
If you want to exclude 0 then change your range to (1,11). The way range works is the lower limit is inclusive where as upper limit is exclusive.
Docs to read more about range: https://docs.python.org/3/library/functions.html#func-range
On an unrelated note, if your lower limit is 0, you need not include the lower limit and write it as range(11) which is same as range(0,11).
Range is inclusive of the first parameter, but does not include the second parameter. The first number in range should be the number you want to start with.
Change your code to
for num in range(1, 11):
if num % 2 == 0:
print(num, ' is an even number!')
else:
print(num, ' is an odd number!')

Python loop and digits [closed]

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 6 years ago.
Improve this question
I would need help to write a program that will load the 5 three-digit numbers.
The program should then print a number that has the highest digit of
hundreds, tens and ones.
Here is my code:
max_num = 0
for number in range(1,6):
a = int(input("Enter five three-digit number: "))
s = a//100
d = (a//10)%10
j = a%10
if(s and d and j) > max_num:
max_num = a
print(max_num)
It only prints the first number.
You need to keep the maximum of each digit:
s = d = j = 0
for _ in range(5):
a = int(input("Enter a three-digit number: "))
s = max(s, a//100)
d = max(d, (a//10)%10)
j = max(j, a%10)
print(s*100+d*10+j)
But you might find it easier to keep them as strings and manipulate individual characters.

Categories