IndentationError: unindent does not match any outer indentation level (Elif) [duplicate] - python

This question already has answers here:
I'm getting an IndentationError. How do I fix it?
(6 answers)
Closed 2 years ago.
def gcd(a,b):
if b == 0:
return a
elif a < b:
return gcd(b,a)
else:
return gcd(b,a%b)
I am new to python, i hope this has something to do with spaces.
Can anyone help me out.enter image description here
Also attached the image. Thanks

If you read the error, it says IndentationError, so you have problem with the indentation. You should know that python depends on indentation to define the scope of each block, so the content of a function must be indented, otherwise it won't be considered as the content of the function.
def gcd(a,b):
if b == 0:
return a
elif a < b:
return gcd(b,a)
else:
return gcd(b,a%b)

Function definitions also have a level of indentation to them, so it should look as follows:
def gcd(a,b):
if b == 0:
return a
elif a < b:
return gcd(b,a)
else:
return gcd(b,a%b)

Related

Python - How do I return True for a positive number? [duplicate]

This question already has answers here:
What is a None value?
(9 answers)
Closed 28 days ago.
I am attempting a practice question on Coursera where the code seems to be throwing me off even though the logic seems fine to me. I am a beginner trying to get my hand on the language but I am running into logical errors.
def is_positive(number):
if number > 0:
return "True"
else:
return "None"
number=-1
print(is_positive(number))
From your comment, I assume that you want to return the values True and None, rather than the strings "True", and "None". To do this, simply remove the quotation marks surrounding the string.
def is_positive(number):
if number > 0:
return True
else:
return None
number=-1
print(is_positive(number))
n = int(input())
print( True if n > 0 else False)
I think this is what you're looking for

return True for recursive function in python [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 1 year ago.
could someone please indicate to me what's wrong with the logic of the simple recursive function, below:
def palindrome (string):
if len(string)<=1:
return True
elif string[0]== string[-1]:
palindrome(string[1:-1])
else:
return False
the False part, works ok, for example :
palindrome('good') the function returns correctly False.
but if it's fed a palindrome like :
palindrome('level') the function returns nothing.
As mentioned in the comments, you don't return the function when calling it recursively, but you should, or the original function call will return None.
Solution:
def palindrome (string):
if len(string)<=1:
return True
elif string[0].lower() == string[-1].lower():
return palindrome(string[1:-1])
else:
return False
I would also add "lower" as capitalization isn't relevant

Why do I keep getting the output I want and an unwanted "none" [duplicate]

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 6 months ago.
def car(wheels,bodies,figures):
car_by_wheels=wheels//4
car_by_figures=figures/2
if figures//2 >= car_by_wheels and bodies >= car_by_wheels:
print(car_by_wheels)
elif car_by_wheels >= car_by_figures and bodies >= car_by_figures:
print(car_by_figures)
elif car_by_wheels >= bodies and car_by_figures >= bodies:
print(bodies)
else:
print("0")
print(car(3,29,54))
I tried a few other examples and the code works fine but I keep getting None. Why?
Your function doesn't explicitly return anything, so it implicitly returns None. So car(3,29,54) does it's printing and returns None, which you then print.

Is there anyway a return statement can contain an if, elif conditionals without the else at the end?

I've been trying to find the answer everywhere but I can't seem to find it. I want to be able to return a statement in my function with an if and (an) elif/s but without an else (just for learning purposes). I am aware that you can do something like this:
def my_func(s):
return "a list" if type(s) == list else "a string" if type(s) == str else "not list or string"
The above shows how python allows you to have an if, as many elifs and an else, but is there any way to be able to have an if and (an) elif/s without the need of an else at the end?
Something to note is that I am speaking about having it on the same line as the return statement, not an expanded one checking on multiple lines.
If it is single line you can have:
def f(x):
if x > 10: return 10
and you do not (explicitly) have a else keyword being used.
Note that this cannot be chained.
Note also that this is anyway equivalent to:
def f(x):
return 10 if x > 10 else None
as any function not returning explicitly otherwise will just return None.
If you consider multi-line constructs, please note that you can do something like:
def f(x):
if x > 10:
return 10
elif x < -10:
return -10
which does not have an explicit else but is equivalent to both:
def f(x):
if x > 10:
return 10
elif x < -10:
return -10
return None
and:
def f(x):
if x > 10:
return 10
elif x < -10:
return -10
else:
return None
So, all in all, there is not much value in not having an else at the end or not using the a if condition else b expression.
It is probably not ideal for readability to chain multiple if-else expressions anyway. Just use multiple lines and enjoy the readability of the code.
If your sole concern is to know if the conditional expression requires else as part of the Python grammar, then the answer is: Yes it does. See the relevant PEP308 for more info.
No it's not possible. See the language specs and the corresponding pep 308
In case of return just add return something if condition else None
As noted, this is a "conditional expression". Expressions always have to resolve to an object but without the else this expression would not. else None is reasonable.
You can write the same idea using return inside the if statement
'' if (type(s) == list): return ....''
In return x, x is an expression which has to be evaluated to some object, no matter what. So you cannot omit the else part of the ternary operator (that's why it's called ternary and not "ternary or binary" ;-) ). That being said, you could shorten your code:
def my_func(s):
return {list: "a list", str: "a string"}.get(type(s), "not list or string")

Unable to print output from a python function [duplicate]

This question already has answers here:
Why does my recursive function return None?
(4 answers)
Closed 7 years ago.
I made a simple function to identify the tennis player who served the last point in a tie-breaker match. Below is my code, which returns the player but when I am printing the result, I get None.
def get_tie_break_point_server(s,tie_break_sum,playerA,playerB):
s = s+1
if(s == tie_break_sum):
return playerB
s = s+1
if(s == tie_break_sum):
return playerB
else :
get_tie_break_point_server(s,tie_break_sum,playerB,playerA)
tie_break_score = [2, 2]
playerA = "A"
playerB = "B"
tie_break_sum = tie_break_score[0] + tie_break_score[1]
print get_tie_break_point_server(1,tie_break_sum,playerA,playerB)
Your function returns None when it reaches the last 'else' block. When a function reaches the end of its code block without an explicit return statement it will implicitly return None. You need to change the last line to
return get_tie_break_point_server(s,tie_break_sum,playerB,playerA)

Categories

Resources