I tried to use continue in a while loop to skip print number but it doesn't work.
num = int(input())
while int(num) > 0 :
num-=1
print(num)
if num == 6:
continue
elif num ==1:
print("8 Numbers Printed Successfully.")
break
#i want to remove number six
Try this
num = int(input())
while num > 0 :
num -= 1
if num == 6:
continue
elif num == 1:
print("8 Numbers Printed Successfully.")
break
print(num)
Your print line should be after the if-else block
num = int(input())
while int(num) > 0:
num -= 1
# print(num) => if you print here, it does not check the condition
if num == 6:
continue
elif num == 1:
print("8 Numbers Printed Successfully.")
break
# print number here
print(num)
First of all, how do you know it's been 8 numbers? The input can be any number. Secondly, if you want to print every number but 6 you need to move the num -= 1 too. Being there, it won't print the first number.
Try this if you don't insist on using continue:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
if num != 6:
print(num)
printed_numbers += 1
num -= 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
Or this, if you want to test continue:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
if num == 6:
num -= 1
continue
print(num)
printed_numbers += 1
num -= 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
Finally, If you're ok with the num -= 1 there:
num = int(input())
printed_numbers = 0
while int(num) > 0 :
num -= 1
if num == 6:
continue
print(num)
printed_numbers += 1
print("{} Numbers Printed Successfully.".format(printed_numbers))
NOTE: I'm using printed_numbers because if the input is less than 6 you will print all the numbers else you'll have one less. You can use this condition instead.
You are print the number before the condition. chnage the code as follows.
num = int(input())
while int(num) > 0 :
num-=1
if num != 6:
print(num)
elif num == 1:
print("8 Numbers Printed Successfully.")
break
#i want to remove number six
Related
I'm trying to add up all the number from 1 to N and print the result and then keep asking the user to enter number till the number zero is entered. I can make it sum the numbers and end the while but can't make it keep asking for more numbers like this: https://pastebin.com/9pWDT6su
num = int(input('N: '))
sum = 0
while num != 0:
while num < 0:
num = int(input('ERROR - N: '))
sum = sum + num
num = num - 1
print('Sum: ', sum)
# If I put this outside the WHILE it'll work but it won't allow me to
# keep adding numbers
num = int(input('N: '))
print('END')
Try this :
num = int(input('N: '))
while num != 0:
sum=0
while num > 0:
sum = sum + num
num = num - 1
print('Sum: ', sum)
num = int(input('N: '))
print('END')
If you want the sum of 1-n for all numbers remove sum=0 inside while
Try this:
num = int(input('N: '))
sum_ = 0
while True:
if num < 0:
num = int(input('ERROR - N: '))
elif num == 0:
break
else:
sum_ = sum_ + num
print('Sum: ', sum_)
num = int(input('N: '))
The sum of digit is running well, but Why is the factorial section not working? Is there any mistakes that I made with the if else, or the break ?
NUM = int (input ("Enter a number "))
if NUM > 1:
for x in range (2, NUM):
if (NUM % x) == 0:
temp = NUM
sod = 0
while temp > 0:
remainder = temp % 10
sod += remainder
temp = temp//10
print (sod)
break
else:
factorial = 1
for I in range (1, NUM + 1,1):
factorial *= I
print (factorial)
else:
temp = NUM
sod = 0
while temp > 0:
remainder = temp % 10
sod += remainder
temp = temp//10
print (sod)
Your break statement on line 12 is outside the if statement, so the for loop will break after the first pass regardless of the value of NUM. Are you sure you didn't mean to indent the break another four spaces?
# example of factorial with prime numbers
num = int(input("Enter a number"))
factorial = 1
if num%2 == 0:
for i in range(1, num + 1):
factorial = factorial*i
print("The factorial of ",num," is",factorial)
Below code checks if the number is prime or odd number. if its prime it finds the factorial of that number and if its odd number then it sums the range of the number. Hope its what you were looking for.
NUM = int(input("Enter number here"))
if NUM > 0:
if NUM % 2 == 0:
factorial = 1
for i in range(1, NUM +1):
factorial = factorial*i
print(factorial)
else:
sum_of_NUM = 0
for x in range(NUM+1):
sum_of_NUM += x
print(sum_of_NUM)
else:
print("something you want to put")
flag = 0
n = int(input('\nEnter whole number to check : '))
i = 2
while i <= (n/2):
if (n%i) == 0:
flag = 1
break
if n == 1:
print('1 is neither prime nor composite')
elif flag == 0:
print(n,' is a prime number.')
elif flag == 1:
print(n,' is not a prime number.')
Upon Entering a number >= 3, the program stalls and the cursor keeps blinking endlessly. I initially tries 277, then 13, then 5, then 3 - none of which gave a result even after a minute.
Entering 2 worked.
There must be something wrong with the code.
Your loop is not changing n or i, which are the conditions on which it stops.
I think the correct code should be:
flag = 0
n = int(input('\nEnter whole number to check : '))
i = 2
while i <= (n/2):
if (n%i) == 0:
flag = 1
break
i += 1
if n == 1:
print('1 is neither prime nor composite')
elif flag == 0:
print(n,' is a prime number.')
elif flag == 1:
print(n,' is not a prime number.')
If I understand correctly, you're trying to check if a number entered is a prime number.
This code works:
# prime numbers are greater than 1, num is the entered number
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
At the end, you can also check for number 1 if you want.
The code below works:
flag = 0
n = int(input('\nEnter whole number to check : '))
i = 2
while i <= (n/2):
if (n%i) == 0:
flag = 1
break
else:
i += 1
if n == 1:
print('1 is neither prime nor composite')
elif flag == 0:
print(n,' is a prime number.')
elif flag == 1:
print(n,' is not a prime number.')
This is because you need to increment i by 1 after every iteration, or your while loop will run endlessly.
I am intended to construct a list of strings from list of numbers. For example, the given list is
list=[1,2,5,25,6]
Desired output:
['Odd', 'Even', Odd, 'multiples of 5 and odd', 'multiples of 2 and even']
My work so far:
list=[]
for num in numbers:
if num % 2:
list.append('Odd')
if not num % 5:
list.append('multiples of 5 and odd')
else:
if not num % 5:
list.append('multiples of 2 and even')
else:
list.append('even')
print(list)
It printed the list but in a wrong way. I was wondering you if you could review my code. Thanks for your help!
l = []
for num in numbers:
if num % 2 == 0:
# Even numbers
# Every even number is a multiple of 2 except number 0
if num == 0:
l.append('Even')
else:
l.append('Multiples of 2 and even')
else:
# Odd numbers
if num % 5 == 0:
l.append('Multiples of 5 and odd')
else:
l.append('Odd')
print(l)
This program is for listing all the prime numbers between 1 and 1000, but my teacher would like me to include 1 in the results.
I tried to change it to say if num >= 1: and for i in range(1,num), but then when I ran it, the only result was 1 is a prime number!. Thanks!
for num in range(1,1001):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num,"is a prime number!")
You should not write for i in range(1, num):, because (any number) % 1 == 0. if num >= 1: can also be removed, because it's always true.
Try the following code:
for num in range(1, 1001):
for i in range(2, num):
if num % i == 0:
break
else:
print num, 'is a prime number'
And remember, technically speaking, 1 isn't a prime number.
Leave your code as is and above the main for loop add:
print("1 is a prime number")
a = int(input("enter the start number"))
b = int(input("enter the end number"))
for i in range(a,b+1):
if i > 1:
for j in range(2,i):
if i % j == 0:
break
else:
print(i,"is a prime number")
import math
n1 = 1000
run_lim = math.ceil(math.sqrt(n1))
prm_num = [2]
for i in range (1,n1+1) :
if i == 1 :
continue
else :
count = 0
for j in range (len(prm_num)) :
if (prm_num[j] <= run_lim) and (i%prm_num[j]) == 0 :
count += 1
if count == 0 :
prm_num.append(i)
print("The Prime Numbers are :- \n")
print(*prm_num, sep=",")
import gmpy2
c = []
for i in range(1,1000):
if gmpy2.is_prime(i)==True:
c.append(i)
print(c)