[Tutor] returning None from function definition
Mats Wichmann
mats at wichmann.us
Sat Oct 24 07:26:13 EDT 2020
On October 24, 2020 12:24:16 AM MDT, Manprit Singh <manpritsinghece at gmail.com> wrote:
>Dear Sir ,
>
>Consider a problem :
>WAP a program that accepts two integers as user input from keyboard (a
>and
>b), and then prints all prime numbers within the range(a, b+1).
>
>def prime(x):
> if x < 2:
> return None
> else:
> for i in range(2, int(x**0.5) + 1):
> if x % i == 0:
> return None
> return True
Consider if you decided to annotate this function, because you want to be clear about the intent. What would you write?
def prime(x: int) -> bool:
would be a declaration that the return is a boolean. If you now run one of the host of checkers available, it would complain that in two places the return type is not expected. Here's a case where the type checking actually can be a bit useful.
--
Sent from a mobile device with K-9 Mail. Please excuse my brevity.
More information about the Tutor
mailing list