Given an integer, , perform the following conditional actions:
If is odd, print Weird
If is even and in the inclusive range of 2 to 5, 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
Every time I run the code, it only executes the else statement else: print("lol") , whether I type any value from 1 to 25.
What's wrong?
n = input("Enter a number here: ")
num = int(n)
if (num % 2) == 0 >= 2 and (num % 2) == 0 <= 5:
print("Not Weird")
elif num % 2 == 0 >= 6 and num % 2 == 0 <= 20:
print("Weird")
elif num % 2 == 0 > 20:
print("Not Weird")
else:
print("lol")
(num % 2) == 0 >= 2 is not the correct way to check if a number is even and in the range you want. If you want to check if num is even and in that range, you'd do something like:
(((num % 2) == 0) and (1 < num <= 5))
As others have pointed out your conditions are not written correctly. Here's the first part to get you started (if the number is odd, and if the number is even and between 2 and 5):
n = input("Enter a number here: ")
num = int(n)
isEven = (num % 2) == 0
if not isEven:
print("Weird")
elif 2 <= num <= 5:
print("Not Weird")
Just as Random Davis said , the problem is in precedence(or whatever it is called sorry XD) so what it does is :
when you check for [(num % 2) == 0 >= 2] : what it does is first checks if num is even[(num % 2)] and then it checks the relation between 0 and 2[0 >= 2] and then equates [ [(num % 2){maybe true or false} == (0 >= 2){always false}]] which results in a LOGICAL ERROR
Rather what you should do is this
(((num % 2) == 0) and (1 < num <= 5)) {just as Random Davis said.}
what it does is checks whether number is even or not[(num % 2) == 0)] and then if number is between 1 and 5[(1 < num <= 5)] and then cheks if both are true or not.
Hope you understand what I am trying to say.
There are a couple of problems with your solution. There is nothing wrong with your if-else statements. They are valid, but are not doing what you want. Looking at the first if:
if (num % 2) == 0 >= 2 and (num % 2) == 0 <= 5:
print("Not Weird")
You first use modulo and test for equality with 0, which is a reasonable test for even vs odd. However, you then compare the boolean result of the even-add test with the number 2. You want to test num with 2, not the boolean of evenness with 2. Testing a boolean result for >= 2 always comes out false. That is True >= 2 is False; and False >=2 is False. Python is smart enough to know that False and (anything else) will be false, so Python won't even evaluate the rest of the statement.
So to test if a number is even and in the range of 2 to 5 inclusive you should want to do:
if num % 2 == 0 and 2 <= num <= 5 :
print("Not Weird")
A few words about Python: Python differs from other languages in that the form "2 <= num <= 5" is a legal construct. It is darn convenient, but atypical among languages. In other languages the initial 2<=num results in a boolean and then then you would get an error comparing a boolean to 5. It is interesting that Python does not throw an error but results in a boolean. Though Python is atypical it is useful. Other languages would require something like
if (2 <= num) and (num <= 5) then # Not a Python statement!
Another oddity is that the and operator is lower precedence than the other comparison operators. In other languages and is higher or equal. The Python rules of precedence let you use "num % 2 == 0 and 2 <= num <= 5", so the evenness test and the double inequality run before the "and", so you get a natureal human result. BUT IT IS DIFFERENT FROM OTHER LANGUAGES. You will probably learn a lot of languages over the years. Appreciate how they work.
I'll let you figure out the rest of the statements in your original solution. However, there is another problem: You don't handle odd numbers in your solution correctly, since you are printing out lol. For debugging you are doing the correct thing, since the printing of "Weird" for odd numbers is an ambiguous result. If you did that on purpose for debugging, CONGRATULATIONS!, you are thinking like a programmer digging through your program.
I submit that your approach is a bit of a brute force approach. The directions do say:
If is odd, print Weird
If is even and in the inclusive range of 2 to 5, 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
As a programmer, you do not have to follow the given directions. You need to formulate a solution that results in an algorithm that meets the specifications. Part of the art of programming is to recognize whether the specs are subject to change, anticipate needs for updates, and even correct some assumptions that the specs make that might be in error. So while this is a simple task, it is rich in teaching. (Kudo's to your instructor).
One way to look at the specs is that it implies non-negative inputs. What is one to do with -4 or 0? Not in the specs. It is fair to look at proposed input and see what it is so you can handle that case. Sometimes other logic is added to reject illegal entries. It isn't specified here, so I would handle it with a comment in the code that input is expected to be a positive non-zero integer.
In addition, there is a certain approach that is a bit more wholistic: If odd print Weird and quit, otherwise you know num is even, so further checking is not needed for evenness. So then you print Weird for num in the range [6..20], otherwise print Not Weird. The big picture is odds print Weird, any number [6..20] prints Weird, everything else prints Not Weird.
if num % 2 == 1 : # Handle odd num
print("Weird") # Any odd prints Weird
else: # Handle even num
if 6 <= num <= 20:
print("Weird")
else:
print("Not Weird")
The point is once you do a test you know womething that can be carried forward in your code. Once you test that something is odd, handle it, and after that the code knows the number if not odd is even, and you don't have to keep testing it. Logically, if odd print and be done. Otherwise (num is even) print "Weird" for num in [6..20], print "Not Weird" otherwise. Rather than 3 tests when a number is even now there is just the one test for num being in the range [6..20].
A few comments are sprinkled in. No testing for input less than 1.
The whole thing could be simplified further
if num % 2 == 1 or 6 <= num <= 20: print("Weird")
else:print("Not Weird")
That does simplify the whole thing.
It is interesting that in programming the simple question of "what is wrong with my statement" can sometimes be addressed with "should I be using this approach?" I've gone far afield with this answer. Originally, what's with my if statement was the issue, but this little gem of a problem is really teaching a lot. (Again, Kudo's to the teacher!)
So, do you transform thw whole question and use an or, or does the first approach work better. In the real world you might have a sense of how the specification may be changed and your solution will be best when it is correct, uses reasonable resources, and is maintainable.
Related
So, I wrote a code to find if a number is PRIME or NOT...
I wrote it in 2 different ways, they are almost same but I just had a doubt. So here it is:
1st code:
num = int(input("Enter the number: "))
lim = num//2 + 1
for i in range(2,lim):
if num % i == 0:
print("Prime!")
break
else:
print("Not Prime!")
2nd Code:
num = int(input("Enter the number: "))
for i in range(2,num):
if num % i == 0:
print("Prime!")
break
else:
print("Not Prime!")
The 1st code takes the input(num) and according to the input sets a limit(which is the half number + 1)
and then checks if the num is divisible by all the numbers in range (2 to lim)
The second one is same but instead of setting a limit it just checks all numbers lower than the input, which means it has to do a little more work...
Now both of these are almost same, the only difference is I saved a line in 2nd one and output efficiency is also better!
Which code would you want me to prefer/
also if this code has any problems, pointing them out would be helpful!
Thanks :)
Explanation
The most important piece of iteration, namely determining whether a number is prime or not, is to keep track of it. Without this process and in the OP's program, a variable is not used to handle this, meaning that he checks whether a number is or isn't prime every single time and concludes at that point. He also uses an else statement which is syntactically incorrect.
To prevent this, we can use a variable to keep track of this. Let's call it isprime. We need to assume that a number will always be a prime unless otherwise said. This can be achieved by setting isprime to default be True, and setting it to be False when we conclude that it is not a prime, because is has a divisor. Finally, we can check this variable at the end and determine whether that number is a prime or not, because it would be set to False if not, or left as True if it is.
Another observation made is that the limit for determining primes can be reduced down to sqrt(n). This is because we do not need to find every factor if it exists, just its lowest corresponding factor. Let's look at an example:
Factors of 24: 2, 3, 4, 6, 8, 12
We can stop checking for the factors right here:
2, 3, 4 | 6, 8, 12, 24
This is because if a number has a factor (such as greater than the square root), it will have a corresponding factor less than the square root. As a result, we can set our limit to be sqrt(n), just for peace of mind + a time complexity of O(sqrt(n)) v. O(n).
As an extra note, sqrt is not inbuilt into Python. You will have to import it from the math library using:
from math import sqrt
Final Code
# Setup
num = int(input("Enter the number: "))
lim = sqrt(num)
isprime = True
# Loop & check
for i in range(2,lim):
if num % i == 0:
isprime = False
break
# Results
if isprime:
print("Prime!")
else:
print("Not prime!")
The logic of the solution is wrong. You gave to switch the "Prime" and "Not Prime" tags. Like follows;
num = int(input("Enter the number: "))
lim = num//2 + 1
for i in range(2,lim):
if num % i == 0:
print("Not Prime!")
break
else:
print("Prime!")
The solution 1 is more efficient because you do not need to do extra
computation to check num//2 + 1. So it is preferable.
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
I'm trying to solve a problem on HackerRank and I sent the code, it worked in amost all the scnarios except for scnario 7 and 3 where they insert 18 and it should return "Weird" and when they insert 20 it should return "Weird" as well according to the website.
The rules of the challenge are:
Given an integer, n, positive number from 1 to 100 , 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 Input Format
A single line containing a positive integer, .
Constraints
Output Format
Print Weird if the number is weird. Otherwise, print Not Weird.
And my code was:
n = 0
n = int(input('Type a number between 1 and 100: '))
odd = False
even = True
# ODD / EVEN
if (n%2) == 0:
n = even #True
else:
n = odd #False
# Is this odd or is this even?
if n == odd:
print ('Weird')
if n == even > 5 < 20:
print('Weird')
elif n == even > 1 < 6:
print ('Not Weird')
else:
print('Not Weird')
I can't see what I did wrong, can you help me to solve it so it can work in all scnarios?
There's a lot to unpick here. As this is an assignment, I'm not going to print the full solution out here, but I will point out the mistakes you've made so you can come to the correct solution.
First issue: reassigning 'n'.
You assign the value from the input to n, then subsequently replace it with the value of odd or even. This means you have lost the original input number. Instead you should assign the result to a new variable (such as 'isEven').
Second issue: uneccesarry 'if'
The result of 'n%2 == 0' is already True/False, depending if n is an even/odd number. So you can simply assign the result, rather than use an 'if' block.
Third issue: multiple operators
Logical operators have an order of operation, and resolution. The statement 'n == even > 5 < 20' makes no logical sense. Instead you should do independent Boolean comparisons, and join them with "and" or "or". E.g. 'if isEven and n < 5 and n> 20'.
I am trying to write code to find whether a given number is odd or even using recursion in Python.
When I execute my code, the recursive function descends down to 0 correctly but then doesn't halt, and keeps going with negative values. I expect it to stop when it reaches 0.
How to make sure the function returns after it reaches zero?
My code:
def odeven(n):
if n%2 == 0:
print("even no. : ",n)
elif n%2 != 0:
print("odd no. : ",n)
elif (n == 0):
return 0
return odeven(n-1)
result = odeven(10)
print("result odd ={}".format(result))
Fixing the most obvious mistake
Your main issue is that the branch elif (n == 0): will never be reached because 0 is even, so 0 gets caught in the first branch if n%2 == 0:
You can fix it by changing the order of the branches:
def odeven(n):
if (n == 0):
return 0
elif n%2 == 0:
print("even no. : ",n)
else: # n%2 != 0
print("odd no. : ",n)
return odeven(n-1)
result = odeven(10)
print("result odd ={}".format(result))
Output:
even no. : 10
odd no. : 9
even no. : 8
odd no. : 7
even no. : 6
odd no. : 5
even no. : 4
odd no. : 3
even no. : 2
odd no. : 1
result odd =0
Further improvements
You can figure out whether a number n is odd or even simply by checking the value of n % 2. There is no need for recursion here.
Presumably this is an exercise that was given by a teacher who wants you to use recursion to figure out whether n is odd or even without using %. This is terribly inefficient and has no use in practice, and is purely an exercise to learn about recursion. In that case, do not use %. Operator % solves the whole problem by itself so if you use it, you don't need to use recursion. It defeats the purpose of the exercise.
Compare the two following functions:
def oddeven_direct(n):
if (n % 2 == 0):
return 'even'
else:
return 'odd'
def oddeven_recursive(n):
if (n == 0):
return 'even'
elif (n == 1):
return 'odd'
elif (n > 1):
return oddeven_recursive(n-2)
elif (n < 0):
return oddeven_recursive(-n)
print(oddeven_direct(10))
print(oddeven_recursive(10))
Note that I did not call function print inside the function. The function returns a result, which is 'even' or 'odd', and doesn't print anything. This is more consistent. If the user calling the function wants to print something, they can call print themselves.
A variation
Notice how the recursive call jumped from n to n-2? This is because I know that n and n-2 will have the same parity (both odd or both even); and we have two base cases, 0 and 1, to which we're guaranteed to arrive when jumping 2 by 2.
If we had jumped from n to n-1 instead, we'd have run into an issue because n and n-1 have different parity (odd and even or even and odd), so we need some way to keep track of this change of parity during the recursion.
This can be achieved by implementing two distinct functions, is_odd and is_even, and using the recursion to express the facts "n is odd if n-1 is even" and "n is even if n-1 is odd".
Since those functions have names that sound like yes/no question, or true/false question, we will make them return True or False, which are called boolean values.
def is_odd(n):
if n == 0:
return False
else:
return is_even(n-1)
def is_even(n):
if n == 0:
return True
else:
return is_odd(n-1)
print('10 is even? {}'.format(is_even(10)))
print('10 is odd? {}'.format(is_odd(10)))
Note that python knows boolean values very well and we can make the code shorter using the two lazy logic operators and and or:
def is_odd(n):
return n != 0 and is_even(n-1)
def is_even(n):
return n == 0 or is_odd(n-1)
print('10 is even? {}'.format(is_even(10)))
print('10 is odd? {}'.format(is_odd(10)))
You get an error, because you call the function inside the function and never stop that function call so it recurses over and over again. Python allows recursion but only until a defined end is reached
To change this limit:
https://www.geeksforgeeks.org/python-handling-recursion-limit/
Also your recursion gets into minus, because you call your function attribute (= (n)) allways one number lower than the function call before:
Your function is now called with
odeven(9)
and so on until it reaches -985, which is in your case the maximum recursion depth which results in:
[Previous line repeated 992 more times]
File "/python/test.py", line 5, in odeven
print("odd no. : ",n)
I am trying to create a program that prints out a list of numbers starting at 0 and leading up to a number the user inputs (represented by the variable "number"). I am required to use "while" loops to solve this problem (I already have a functional "for" loop version of the assignment). The program should mark anything in that list divisible by 3 with the word "Fizz," divisible by 5 with the word "Buzz," and anything divisible by both with "FizzBuzz" while also including unlabeled numbers outside of those specifications.
Every time I run this program, it ignores the conditions and just prints the word "FizzBuzz" however many times is represented by the number inputted. (I typically use 15 because it has at least one example of each condition, so that means I get 15 "FizzBuzz"s in a row).
To find out why it was doing that, I used print(i) instead of the rest of the program under the first conditional and it gave me 15 counts of the number 0, so there is reason to believe the program is completely ignoring the range I gave it and just outputting copies of i based on the user number input.
Any help would be appreciated!
number = int(input("Enter a Number"))
i = 0
while(i < number + 1):
if number % 3 == 0 and number % 5 == 0:
print("Fizzbuzz")
elif number % 5 == 0:
print("Buzz")
elif number % 3 == 0:
print("Fizz")
else:
print(number)
i += 1
print ("Done!")
You meant to check the divisibility of i, which increments every loop, not of number which doesn't change.
You also meant to print(i) in the else clause.
I am new to python and i am still strugling to understand how the sytnax works, how you need to allign your If and else to make it work correctly. How do i really know which else goes with which if? especially when using nested code blocks.
In the code below for the else followed by the comment Prime! from what i understand that else goes with the statement if (n % div == 0): but then why is it alligned with the FOR statement instead?
the last else statement i think goes with if n == 2: but the else is not alligned with it, instead it is after. For the same statement if n == 2: why is n += 1 alligned before pime_count +=1 and not after it.
I understand that the placement of the Else and if is very important because if i decided to move any of them the code stops working. What i can't seem to understand is how does python know which else goes with which if, if the allignment doesnt seem to be consistent.
#!/usr/bin/env python
#
# Problem Set 1a
#
# A program that computes and prints the 1000th prime number.
# Finds primes using trial division (least efficient method)
#------------------------------------------------------------
prime_count = 0
n = 2
while (prime_count <= 1000):
#if even, check for 2, the only even prime
if (n % 2 == 0):
if n == 2:
prime_count += 1
n += 1
else:
# number is odd, possible prime
for div in range(3, n, 2):
if (n % div == 0):
# not a prime
n += 1
break
else:
# prime!
prime_count += 1
if prime_count == 1000:
print "The 1000 prime is", n
else:
n += 1
The rule is very simple: the else clause must have the same indentation as the statement it refers to (most commonly, an if statement).
Now, here:
for div in range(3, n, 2):
if (n % div == 0):
# not a prime
n += 1
break
else:
...
you are not using if-else, you are using for-else!
This construct means "execute the else block unless the loop has terminated through a break".
See for ... else in Python for a discussion.
An if goes with an else at the same indentation, so long as there are no other things at lower indentation between them. They have to be in the same "block". However, in your case, the else that's followed by # prime! is not actually joined to an if at all, but rather to the for div in range(3, n, 2): loop before it!
An else attached to a for loop means "execute this code if the for loop completed without hitting a break statement". It can be useful sometimes, but it is often confusing for people who haven't encountered it before!
I think this can help you to understand how python indentation works http://psung.blogspot.com/2007/12/for-else-in-python.html
In a construct like this one:
for i in foo:
if bar(i):
break
else:
baz()
the else suite is executed after the for, but only if the for terminates normally (not by a break).
In other situations else goes after if
There are 2 rules which are fairly simple,
The indent of the if and else have to be the same
for x in range(15):
if x > 10:
print 'x is more than 10'
else:
print 'x is less than or equal to 10'
Nothing with an indent lower than or equal to that of if and elseshould come in between them
So, this is invalid/ will raise a SyntaxError.
for x in range(15):
if x > 10:
print 'x is more than 10'
print x
else:
print 'x is less than or equal to 10'
Also, As stated in PEP 8
Use 4 spaces per indentation level.
for div in range(3, n, 2):
if (n % div == 0):
# not a prime
n += 1
break
else: # at same indent as for
# prime!
Also, your indent above means for...else loop is made (here else clause is executed if the for loop is exited using break), not if..else.