+ 6

Whats wrong with this code?

I want to create a function which give me a list with all numbers smaller than the entered one if they are divisible by 3 and 5. But I don't get the output I want. Any hints? Thanks! num = int(input()) list=[] def all_smaller_num(x): while x > 0: x -= 1 if (x % 3 == 0) and (x % 5 == 0): list.append(x-1) return list all_smaller_num(num) print(list)

30th Dec 2021, 5:11 PM
David Lau
David Lau - avatar
14 Answers
+ 6
Your return statement is in if block so on first append() , control goes back to calling statement. Returns from function. So remove from loop and at end of function.
30th Dec 2021, 5:32 PM
Jayakrishna 🇮🇳
+ 4
You don't need to return the `list`, it's on global scope already. Unless you assign the return value to something (e.g. a variable), or pass it to a function (as argument), there's no necessity to issue a `return`. You can still opt to define the list inside the function though, that way it makes sense to issue a return.
30th Dec 2021, 5:51 PM
Ipang
+ 4
#A number fully divisible by both 3 and 5 is also a number fully divisible by 15. So it might worth considering to use two while...loops to accomplish this more efficiently. #The first while...loop searches for a number that is less than <x> and be fully divisible by 15. The second while...looo adds the matching numbers to the list. def all_smaller_num(x): # find first number fully divisible by 15 while x > 14 and x % 15: x -= 1 # add matching numbers to <lst> lst = [] while x > 14: lst.append(x) x -= 15 return lst print( all_smaller_num( 1000 ) )
30th Dec 2021, 6:23 PM
Ipang
+ 3
You could eliminate the loops and the filtering of unwanted values. Simply generate the values you want by using range. Look: num = int(input()) a = list(range(3, num+1, 3)) + list(range(5, num+1, 5)) a = list(set(a)) print(a)
30th Dec 2021, 7:57 PM
Brian
Brian - avatar
+ 2
num = int(input()) list=[] def all_smaller_num(x): while x > 0: x -= 1 if (x % 3 == 0) and (x % 5 == 0): list.append(x-1) #Why x-1 here? return list all_smaller_num(num) print(list)
30th Dec 2021, 5:22 PM
CGM
CGM - avatar
+ 2
Oh yes, that makes no sense indeed. I thought it would be necessary to append the next smaller integer to the list, but this should execute by the x-=1, right? But nevertheless: when I change the x-1 to x I still get only on number in the list as output :-(
30th Dec 2021, 5:29 PM
David Lau
David Lau - avatar
+ 2
And please avoid using any of the built-in class name for variable names. You may accidentally shadow and override the built-in class definition.
30th Dec 2021, 5:52 PM
Ipang
+ 1
ah I just realized, it is returning the list in the first match. return must go outside the loop
30th Dec 2021, 5:33 PM
CGM
CGM - avatar
+ 1
Oh yes, now it works! Thank you both for your help! From now on I will remember for ever: return outside the loop and in one level with the function!
30th Dec 2021, 5:41 PM
David Lau
David Lau - avatar
0
Omit the (x-1). It should be done. The code should look like this: num = int(input()) list=[] def all_smaller_num(x): while x > 0: x -= 1 if (x % 3 == 0) and (x % 5 == 0): list.append(x) return list all_smaller_num(num) print(list) \*Please correct me if I'm wrong, appreciated it. Thanks! */
30th Dec 2021, 11:47 PM
Jevon Harry
Jevon Harry - avatar
0
Example: number_of_likes = int(input())
1st Jan 2022, 1:57 PM
Filip Jevtić
Filip Jevtić - avatar
0
def FN (ins): a = False n = 2; while (not a): um = set ( []) for i,item in enumerate (ins): ni = item/n if(ni not in um): else: break つく um.add(ni) if(i==len (ins)): a = True 4 if not a: 5 V 6 n =n+1 7 8 return n 9 20 if __name__ == '_ _main__': 21 22 inp = list (map (int, input().rstrip().split() n = inp.pop(0) 23 24 ar = inp print (FN (ar)) Test Results Custom
30th Jan 2022, 7:08 AM
S.Sharath
0
link your actual code. it is easier for you and the people trying to help.
30th Jan 2022, 7:17 AM
Bob_Li
Bob_Li - avatar
- 1
Hello
1st Jan 2022, 1:58 PM
Shohruh Ibodullayev
Shohruh Ibodullayev - avatar