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 2 years ago.
Improve this question
I tried to use boolean instead of 'break' , but I could not manage to do that. Is there a way to edit this code without 'break' ?
Primes:
lower = int(input("Enter the lower bound: "))
upper = int(input("Enter the upper bound: "))
for num in range(lower,upper+1):
if num>1 :
for i in range(2,num):
if(num%i) == 0:
break
else:
print(num, end =", ")
You want to get rid of the break and preserve the same behavior of exiting the inner loop as soon as num % i == 0. This is one way to do it, using a boolean flag and replacing the for with a while so we can add one extra condition for the iteration:
for num in range(max(2, lower), upper + 1):
i = 2
prime = True
while i < num and prime:
if num % i == 0:
prime = False
i += 1
if prime:
print(num, end=", ")
You could replace break with pass. I think that will do what you want.
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 14 days ago.
Improve this question
n = [1,2,3,4,5,6]
while True:
num = (input("enter your choice(from 1 to 6) "))
if num not in str(n) or num.isdigit == "False":
print("enter valid chice ")
os.system('cls')
pr()
else:
break
I want it to loop if the input is string and not in n
n = [1,2,3,4,5,6]
while True:
num = input("enter your choice(from 1 to 6) ")
if not num.isdigit() or int(num) not in n:
print("enter a valid choice ")
else:
break
The issue in the original code was checking isdigit as a string instead of calling the method on num. Also, the condition to check if num is not in n was incorrect. The corrected code checks if num is not a digit and also checks if the integer form of num is not in n.
Your code is convoluted, here's a simpler version:
# use try/except to get an int, or set our var outside the bounds in the while loop
try:
num = int(input("Enter your number (1-6):"))
except ValueError:
num = 0
# while our input isn't in the 1-6 range, ask for a number
while num not in [1,2,3,4,5,6]:
# same as before, if this is not a number, just set it to something that's not in [1-6] so we stay in the while loop
try:
num = int(input("Enter a valid choice please (1-6):"))
except ValueError:
num = 0
# your code here
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 4 months ago.
Improve this question
num=int(input('Enter a number:'))
while num !=0:
if num>0:
if num%2==0 and num%7==0:
print('The number is a multiple of two and seven.' )
num=int(input('Enter a number:'))
elif num%2==0:
print('The number is a multiple of two.' )
num=int(input('Enter a number:'))
elif num%7==0:
print('The number is a multiple of seven.' )
num=int(input('Enter a number:'))
else:
print('The number is not a multiple of two or seven.' )
num=int(input('Enter a number:'))
else:
continue
else:
print('The user did not enter negative numbers.' )
Answer:
Enter a number:
>>> 5
The number is not a multiple of two or seven.
Enter a number:
>>> 6
The number is a multiple of two.
Enter a number:
>>> -8
The question is not very clear but I think I understand what you'd like to achieve.
Your solution is partially correct, but the "continue" inside the else is not followed by a new insertion of "num". So the input "num" is never changed when you insert a negative number.
Also the While-else clause has no sense in this case, if you want to count the negative number inserted you can just put a counter inside the first else clause.
You should change the code and move the "input" inside the while:
num = 1
negative_counter = 0
while num !=0:
num=int(input('Enter a number:'))
if num>0:
if num%2==0 and num%7==0:
print('The number is a multiple of two and seven.' )
elif num%2==0:
print('The number is a multiple of two.' )
elif num%7==0:
print('The number is a multiple of seven.')
else:
print('The number is not a multiple of two or seven.' )
else:
negative_counter = negative_counter + 1
continue
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 11 months ago.
Improve this question
Create a counter that takes in a number from the user, counts from 0 to that number, tells the user as it counts if each number is even or odd, then sums all the numbers together and prints the sum.
The following code and comments are a starting point. The text following each # is a comment, you will need to replace the comments with the necessary code:
count = 0
sum = 0
num = 0
# Ask user for a number
#All code lines that need to be inside the while loop, need to be tabbed over once.
while (count <= num):
# add count to sum
# Use the following if statement along with the remainder
# operator (modulus, %) to check if a number is even or odd.
if count % 2 != 0:
#print the number is odd
else:
#print the number is even
# update counter for the while loop exit condition
count = count + 1
#print the sum
Using for loop
count=0
sum=0
for i in range(0,n+1):
if (i%2==0):
print("Number is Even")
else:
print("Number is Odd")
sum+=i
print(sum)
Using While loop:
count=0
sum=0
while(count<n):
if(count%2==0):
print("Number is Even")
else:
print("Number is odd")
count+=1
sum+=count
print(sum)
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 3 years ago.
Improve this question
Write a function to enter from the keyboard a series of numbers between 1 and 20 and save them in a list.In case of entering an out-of-range value the program will display an error message and ask for a new number.To finish loading you must enter -1.La function does not receive any parameters, and returns the loaded list (or empty, if the user did not enter anything) as the return value.
def funcion():
list = []
num = 0
while num != -1:
num = int(input("Enter a number between 1 and 20: "))
while num > 20 :
num = int(input("Please re-enter the number: "))
list.append(num)
return lista
result = funcion()
print(result)
My question is how I do not show the -1 in the list
It's easier to have an infinite loop and break out of it when the user enters -1:
def funcion():
lista = []
num = 0
while True:
num = int(input("Enter a number between 1 and 20: "))
while num > 20:
num = int(input("Please re-enter the number: "))
if num == -1:
break
else:
lista.append(num)
return lista
result = funcion()
print(result)
The trick is saving the list except the last item like this:
return lista[:-1]
you can read more here:
https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
The minimal change to your code would just use the list slicing syntax to return all elements of lista apart from the last one:
return lista[:-1]
See [1] below. This is a pythonic way of writing the slightly more intuitive, but more verbose statement:
return lista[:len(lista)-1]
... which in turn is short for the even longer:
return lista[0:len(lista)-1]
You seem to be new to python. I really recommend looking up "list slicing" and "list comprehensions": if you already know a programming language that doesn't have these, once you learn these, you'll wonder how you ever did without them!
[1] http://knowledgehills.com/python/negative-indexing-slicing-stepping-comparing-lists.htm