Task
Given an integer, n, perform the following conditional actions:
If n is odd, print Weird
If n is even and in the inclusive range of 2 to 5, print Not Weird
If n is even and in the inclusive range of 6 to20 , print Weird
If n is even and greater than20 , print Not Weird
import math
import os
import random
import re
import sys
if __name__ == '__main__':
n = int(input().strip())
if (n %2 != 0) or (n %2 ==0 and range(6,21)):
print( 'Weird')
elif(n %2 == 0 and range (2,6)) or (n %2 ==0 and n >20):
print('Not Weird')
this is the output
I think this works:
...
if (n % 2 != 0) or (n in range(6, 21)):
print('Weird')
elif (n in range(2, 6)) or (n > 20):
print('Not Weird')
You can consider the below approach to get the required output.
import sys
if __name__ == '__main__':
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n >= 2 and n <= 5:
print("Not Weird")
elif n >= 6 and n <= 20:
print("Weird")
elif n > 20:
print("Not Weird")
Related
I need to test whether n is a multiple of 2 and then divide the number by 2. If the number isn't a multiple of 2, then I have to do 3*n+2.
How do I make it loop so I can get the following: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1?
Here's my current code:
n=12
while n!=0:
if n%2==0:
print (n)
n=n/2
if n!=n%2:
if n !=1:
n = 3*n+2
else:
break
print(n)
First note is that it is 3*n+1, second one is that you have to write n=n/2 in your first if.
Overall this code is what you want:
n = 12
while n != 0:
if n % 2 == 0:
print(n, end=' ')
n = n / 2
elif n % 2 != 0:
if n != 1:
print(n, end=' ')
n = 3 * n + 1
else:
print(1)
break
Or this:
n = 12
while n > 1:
if n % 2 == 0:
print(n, end=' ')
n = n / 2
elif n % 2 != 0:
if n != 1:
print(n, end=' ')
n = 3 * n + 1
else:
print(1)
Good luck
First of all your formula of 3*n + 2 will lead to infinite loop. According your sample output, it needs to be 3*n + 1.
n = 12
while n >= 1:
print(n, end=' ')
if n == 1:
break
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
Code runs fine in jupyter notebook but gives error in hacker rank
n=int(input())
for i in range(0,99):
n= n+1
if (n % 3 == 0 and n % 5 == 0):
print('FizzBuzz')
elif (n % 3 == 0 and n % 5 != 0):
print('Fizz')
elif (n % 3 != 0 and n % 5 == 0):
print('Buzz')
else:
print(n)
I guess, that is the problem - n = int(input())
Maybe, hacker rank does not support input construction. Try to replace it for random value.
Im trying to write a function that outputs all the prime numbers between a certain range that ends with 7. However when I run the program it gives me numerous outputs of the same number. I think it has something to do with the loop but I dont know what to fix.
lower=int(input('enter lower bound: '))
upper=int(input('enter upper bound: '))
for i in range(lower, upper + 1):
if i > 1:
for x in range(2, i):
if (i % x) == 0:
break
elif i%10==7:
print(i)
For now the elif is related to this if, so when you're testing 17 for all number that are not a divisor of 17 you're going into the elif and print it
if (i % x) == 0:
break
elif i%10==7:
print(i)
Fix with a for-else
Use the for-else, if the loop encounter no break it goes into the else (if there is one), here you can add your condition
for i in range(lower, upper + 1):
if i > 1:
for x in range(2, i):
if (i % x) == 0:
break
else:
if i % 10 == 7:
print(i)
Improve
Then you can imagine, to check prime only for ..7 numbers, this will avoid a huge amount of computing. Also to remove the if i>1 which is quickly useless, use max on the lower bound, if you set like a negative value, it'll start at 2, no empty run, and end the testing loop at sqrt(i) because you won't find a divisor after its square root
from math import sqrt
for i in range(max(lower, 2), upper + 1):
if i % 10 == 7:
for x in range(2, int(sqrt(i))):
if (i % x) == 0:
break
else:
print(i)
for i in range(min(lower,2), upper + 1):
if i % 10 == 7:
for x in range(2, i):
if (i % x) == 0:
break
else:
print(i)
i am doing a challenge but for some reason every time i run it says 3 outta of 7 test cases are incorrect and don't know why? everything seems in order. Here is the challenge if Task
Given an integer, , perform the following conditional actions:
If is odd, print Weird
If is even and in the inclusive 2 range of 5 to , print Not Weird
If is even and in the inclusive range of 6 to 20, print Weird
If is even and greater than 20, print Not Weird
My code below:
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n % 2 == 1 and n in range(2,5):
print("Not Weird")
elif n % 2 == 1 and n in range(6,20):
print("Weird")
elif n > 20:
print("Not Weird")
Try this
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n in range(2,6):
print("Not Weird")
elif n in range(6,21):
print("Weird")
elif n > 20:
print("Not Weird"
To include 5 and 20 in range you need to specify it as number + 1. Range does not include the last number. Also, there is no need to check for even condition every time in the else part as the control jumps to else when if fails!.
n = int(input().strip())
if n % 2 != 0:
print("Weird")
else:
if n in range(2,6):
print("Not Weird")
elif n in range(6,21):
print("Weird")
elif n > 20:
print("Not Weird")
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)