How to check if all the digits of a number are odd in python?

前端 未结 4 967
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 17:41

I was told to solve a problem in which I would have to find out the number of 4-digit numbers which are all composed of odd digits. I tried the following python code:

         


        
4条回答
  •  Happy的楠姐
    2021-01-26 18:28

    I believe something like this would work if you're looking to model what you already have, but I echo the comment by yotommy that some intuitive multiplication would do the trick.

     for a in range(1111,10000):
           allOdd = True
           for b in str(a):
               if int(b) % 2 == 0:
                   allOdd = False
           if(allOdd):
              new_list.append(a)
    

提交回复
热议问题