[Tutor] Problem of finding the first odd number in the list

Peter Otten __peter__ at web.de
Fri May 7 02:59:19 EDT 2021


On 07/05/2021 01:45, Manprit Singh wrote:

> Let us consider  a list lst = [2, 4, 7, 9, 0], the first odd number in the
> list is 7.
> The code i have written for it is given below:
> for i in lst:
>      if i%2 != 0:
>          break
> print("First odd number in the list is", i)
> 
> Gives the right answer 7
> 
> The target variable i will retain the value 7 as it is the value of i when
> break statement executes . Is my understanding correct ? 

Yes.

> the way i have
> solved the problem is correct ?

No.

Hint: What happens if the list doesn't contain any odd numbers? If it 
doesn't contain any elements?



More information about the Tutor mailing list