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")
Related
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")
I have a problem with the task. The task is:
We say that number 1 is a super number. If a number x is super, then
the numbers 2x and 3x are also super. For example, since the number 1
is super, then the numbers 2 and 3 are super. As 2 and 3 are super,
then the numbers 4, 6 and 9 are super, and so on. At the same time,
numbers 10 and 7 are not super. Write a program that asks the user to
enter a natural number n. The program prints whether the entered
number is super.
And this is what I have done so far
num = int(input("Enter a natural number "))
if num <= 0:
print("That is not a natural number")
else:
if num % 5 == 0 or num % 7 == 0 or num % 11 == 0 or num % 13 == 0:
print("Number is not super.")
elif num == 1 or num % 2 == 0 or num % 3 == 0 or num % 8 == 0 or num % 9 == 0:
print("Number is super")
else:
print("Number is not super.")
The problem is that for some numbers like 62 it says that it is a super number, but it ain't..
Just following the definition of super number:
def is_super(k):
if k == 1: return True
if k % 2 == 0:
return is_super(k / 2)
if k % 3 == 0:
return is_super(k / 3)
return False
Some testing
print(is_super(9))
True
print(is_super(14))
False
print(is_super(32))
True
print(is_super(62))
False
To me it looks like you jumped in without first figuring out how you'd work it out manually.
Personally, I think the easiest way to start this would be recursively, though it'll be very inefficient with large numbers, so it's up to you if you then want to go and optimise it after doing the first version. I got it working pretty easily, but since it's a problem you need to solve, I'm not going to just copy and paste the answer.
It works something like this (psuedocode):
def is_super(i):
if i is below 1: not a super
if i is 1: is a super
if i is above 1: check if i/2 or i/3 is a super
There are some nice recursive solutions here, I'll propose a non-recursive one. As some of the comments hint, super numbers have a tell-tale factorization: they are all of the form 2x * 3y for x, y >= 0, and every integer of that form is a super number. This should become clear after some study because the only way to get a super number is to multiply an existing one by 2 or 3, and we start with 1. That leads to the following code to test for superness:
def is_super(n: int) -> bool:
if n < 1:
return False
while n % 3 == 0:
n //= 3
return n & (n - 1) == 0; // Suggested by #KellyBundy
I tried to find shortcut way to test for superness but failed to find anything better than this.
num = int(input("Enter a natural number "))
if num <= 0:
print("That is not a natural number")
else:
if num % 3 == 0:
while num % 3 == 0:
num = num / 3
print(num)
if num == 1 or num % 2 == 0 or num % 3 == 0:
print("Number is super")
else:
print("Number is not super")
elif num % 2 == 0:
while num % 2 == 0:
num = num / 2
print(num)
if num == 1 or num % 2 == 0 or num % 3 == 0:
print("Number is super")
else:
print("Number is not super")
else:
print("Number is not super")
This is what I've done so far but I don't think it will work for large numbers ?
I want to check the value range of an input number, then print the correct "size" (small, medium or large). If the value is out of my acceptable range, then I want the else statement to print out that the number is not valid.
Minimal example for my problem:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
if 5 <= n < 10:
a = "medium"
if 10 <= n <= 20:
a = "large"
print("this number is",a)
else:
print("thats not a number from 0 to 20")
According to Google, this is a problem with indentation. I've tried multiple ways of indenting this; I can fix the syntax, but I can't get the logic correct.
Let's fix your immediate issue: you have an else with no corresponding if statement. Syntactically, this is because you have an intervening "out-dented" statement, the print, which terminates your series of ifs.
Logically, this is because you have two levels of decision: "Is this a number 0-20?", and "Within that range, how big is it?" The problem stems from writing only one level of ifs to make this decision. To keep close to your intended logic flow, write a general if on the outside, and encapsulate your small/medium/large decision and print within that branch; in the other branch, insert your "none of the above" statement:
n = int(input("number= "))
if 0 <= n <= 20:
if n < 5:
a = "small"
elif n < 10:
a = "medium"
else:
a = "large"
print("this number is", a)
else:
print("that's not a number from 0 to 20")
You should try something like
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
elif 5 <= n < 10:
a = "medium"
elif 10 <= n <= 20:
a = "large"
else:
a = "not a number from 0 to 20"
print("this number is",a)
The print statement before the else statement needs to either be removed or indented to match:
a= "large"
You've syntax (indentation) error:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
if 5 <= n < 10:
a = "medium"
if 10 <= n <= 20:
a = "large"
#print("this number is",a) indentation error in this line
else:
print("thats not a number from 0 to 20")
You can use also use following code
n = int(input("number= "))
if 10 <= n <= 20:
a = "large"
print("this number is",a)
elif 5 <= n < 10:
a = "medium"
print("this number is",a)
elif 0 <= n < 5:
a = "small"
print("this number is",a)
else:
print("thats not a number from 0 to 20")
The problem is with the print statement.
It is indented on the same level as the if block and thus, the if block ends on line containing the print statement.
Thus, the else on the next line is incorrect.
To achieve what you are trying, you should do something like this:
n = int(input("number= "))
if 0 <= n < 5:
a = "small"
elif 5 <= n < 10:
a = "medium"
elif 10 <= n <= 20:
a = "large"
else:
print("not between 0 and 20")
print("The number is", a)
The print statement needs to be placed after the else block. Also, its best to use elif statements than if statements in a situation like this.
n = int(input("Enter a number between 0 and 20: "))
if 0 <= n <= 5:
a = "small."
elif n <= 10:
a = "medium."
elif n <= 20:
a = "large."
else:
a = "invalid / out of range."
print("This number is ", a)
See, I started learning Python today and I am stuck in a situation.
I was solving a very simple question and I completed it but there is a problem.
First of all, let me tell the question and the constraints. Here is the question:
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 to 20, print Weird
If n is even and greater than 20, print Not Weird
Here is my code:
n = input("")
if n%2!=0 :
print ("Weird")
elif n%2==0 :
for n in range(3,4,5) :
print ("Not Weird")
elif n%2==0 :
for n in range(6,21) :
print ("Weird")
else :
if (n%2==0 & n>20) :
print ("Not Weird")
I compiled the code, everything is fine but for the input 18 and 20 I must get Weird as output but I am getting Not Weird. Can anyone help me?
Were you asked specifically to use for loops? If not you can try this!
n = int(input(""))
if n%2==1:
print("Weird")
elif n%2==0 and n in range(3, 6):
print("Not Weird")
elif n%2==0 and n in range(6, 21):
print("Weird")
elif n%2==0 and n>20:
print("Not Weird")
You have 2 elif with same condition it shouldn't.
Change it or remove one elif
n = input("")
if n%2!=0 :
print ("Weird")
elif n%2==0 :
if n>20:
print ("Not Weird")
for n in range(3,4,5) : #range shouldn't be like this. But for your understanding i leave it as it is
print ("Not Weird")
for n in range(6,21) :
print ("Weird")
Final else also not needed
Your have a problem with range(3,4,5). list(range(3,4,5)) is [3]..probably not what you expected...range is start, stop, step.
You could do for n in [3,4,5] or range(3,6)
Your main problem is that you keep re-defining n in your for loops, thus at each conditional statement after the first for loop your n is not what was initially given.
In your two cases of n=18 and n=20, the first condition n%2!=0 is False, so then the second condition is tested. n%2==0 is True, so the for n in range(3,4,5): loop is executed. The first (and only) iteration of that loop sets n to 3 and prints 'Not Weird'. Since n is now 3, none of the remaining conditions in you code are True, so they do not execute.
The following would be much simpler given your requirements:
def isweird(n):
if n%2 or n in range(6,21):
return 'Weird'
if n in range(2,6) or n > 20:
return 'Not Weird'
for n in range(23):
print(n, isweird(n))
Output:
0 None
1 Weird
2 Not Weird
3 Weird
4 Not Weird
5 Weird
6 Weird
7 Weird
8 Weird
9 Weird
10 Weird
11 Weird
12 Weird
13 Weird
14 Weird
15 Weird
16 Weird
17 Weird
18 Weird
19 Weird
20 Weird
21 Weird
22 Not Weird
There are a number of problems with the code you've provided.
The most obvious is that you have the same condition repeated in two branches of an if...else chain. The second such condition,
elif n%2==0:
for n in range(6,21) :
will never execute.
Also, the use of for loops in those branches is incorrect. Those should be conditionals. You use a loop when you want to repeat an action, but here you have one variable and you want to ask a question about it. That's an "if":
elif n%2==0 :
if n in range(3,4,5) :
print ("Not Weird")
But that leads to the next bug, which is the use of range. As written, you're specifying the range of numbers starting with 3, running up to 4 (not inclusive), in steps of 5. That would be the range containing 3.
To specify the inclusive range from 2 to 6, you would write
elif n%2==0 :
if n in range(2,7):
print ("Not Weird")
Ranges are inclusive on the bottom end and exclusive on the top end. This seems counterintuitive at first, but it's generally what you want when working with zero-indexes. For example, you would like range (10) to have 10 members, and to start with zero, hence contain all of the base-ten digits.
You can also write this as
elif n%2==0 :
if 2 <= n <= 6:
print ("Not Weird")
which would be my preference.
So, let's walk through your code line by line and see what's happening in the case of an input of 18:
if n%2!=0 :
print ("Weird")
18 modulus 2 is equal to 0, so this print statement will not be reached.
Next:
elif n%2==0 :
for n in range(3,4,5) :
print ("Not Weird")
Hello, this one matches! Therefore, the for statement will be executed. No other statements in your if / elif / else chain will be reached for the value 18, because you found a match here.
However, you're not going to see what you expect, because the for statement does not do what you think it does. You're thinking that it's going to look at n and for a value of n that's found in the the list [3,4,5], the value Not Weird will be printed.
That's not what for does. Instead, n will be changed to one each of the values in the range, in turn, and the code inside the loop will be executed with n set to that value. Now, you're wondering why, if this is the case, you don't see the Not Weird printed three times? The reason is that range is a function and it doesn't return a list of the supplied numbers, it returns a bunch of numbers starting with (in your example) 3, and ending with 4, but taking only every 5th number. By coincidence, this gives a range with only 1 value (the value 3) which is why Not Weirdappears only once.
Here is a corrected and annotated version of your code:
if n%2 != 0:
print ("Weird")
else:
#Don't need elif, there are only two cases odd, or not.
#But, inside this branch we have three possibilities.
if n in [2, 3, 4, 5]:
#I just enumerate here, instead of using range. You forgot about 2, BTW
print ("Not Weird")
elif n in range(6,21)
# Note the use of if (or elif), not for.
print ("Weird")
else:
print ("Not Weird")
n = int(input())
if n % 2 != 0:
print('Weird')
elif n % 2 == 0 and n in range(2, 6):
print('Not Weird')
elif n % 2 == 0 and n in range(5, 21):
print('Weird')
elif n % 2 == 0 and n > 20:
print('Not Weird')
This will work....the key point there is inclusive.
n = int(input().strip())
check = {True: "Not Weird", False: "Weird"}
print(check[
n % 2 == 0 and (
n in range(2, 6) or
n > 20)
])
This will be the shortest solution without using if else.
Here is a direct solution:
n = int(input())
if n%2==0 and n not in range(6,21):
print("Not Weird")
else:
print("Weird")
num = input()
n = int(num)
if n%2 !=0:
print('Weird')
if n%2==0 and n in range(2,6):
print('Not Weird')
if n%2==0 and n in range(6,21):
print('Weird')
if n%2==0 and n>20:
print('Not Weird')
else:
pass
Try this:
n= int(input("")
if n%2==1:
print("Weird")
elif n%2==0 and n in range(3, 6):
print("Not Weird")
elif n%2==0 and n in range(6, 21):
print("Weird")
elif n%2==0 and n>21:
print("Not Weird")
n=3
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")
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)