![]() |
num+1 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: num+1 (/thread-38463.html) Pages:
1
2
|
num+1 - astral_travel - Oct-16-2022 hi, in the following code: __author__ = 'jeffreyhunt' num = int(input("Please choose a number to divide: ")) listRange = list(range(1,num+1)) divisorList = [] for number in listRange: if num % number == 0: divisorList.append(number) print(divisorList)what is the meaning of num+1 ? RE: num+1 - ndc85430 - Oct-16-2022 The stop parameter is excluded (that is, the final value in range(1, num) will be num - 1 ). So if you want a range from 1 up to and including num , you need to pass num + 1 for stop .Docs for ranges: https://docs.python.org/3/library/stdtypes.html#typesseq-range. RE: num+1 - astral_travel - Oct-16-2022 alright ! i got it ! okay i have another question: when i execute the following code: user_number = int(input("please choose a number to check: ")) list1 = range(2, user_number+1) list2 = [] for x in list1: if user_number % x == 0: list2.append(x) print(list2)it gives me the following output: now, why does it give me the whole process of getting to 30 ?i mean, why doesn't it just write the last result ? (the last line) ? like, simply: [2, 3, 5, 6, 10, 15, 30] RE: num+1 - snippsat - Oct-16-2022 print is inside the loop,so will show results for every iteration. user_number = int(input("please choose a number to check: ")) list1 = range(2, user_number+1) list2 = [] for x in list1: if user_number % x == 0: list2.append(x) print(list2) RE: num+1 - astral_travel - Oct-16-2022 yep - that fixed it, amazing how little things make big changes... thank you very much to both of you ! RE: num+1 - astral_travel - Oct-16-2022 can you help me with this also ? when this code is being executed: def tri_recursion(k): if(k > 0): result = k + tri_recursion(k - 1) print(result) else: result = 0 return result print("\n\nRecursion Example Results") tri_recursion(6)it gives the following output: but i don't know how this code manufactured this numbers...couldn't wrap my head around this...is there an explanation to it RE: num+1 - ndc85430 - Oct-16-2022 What, in particular, are you stuck on? What is your current understanding of how it works? RE: num+1 - Yoriz - Oct-16-2022 You can step through the code using this code visualizer RE: num+1 - astral_travel - Oct-16-2022 Yoriz thank you very much for the code visualizer, it helps allot ! ndc, what i don't understand is the "game" of numbers that runs in the loop, i don't totally understand the concept of recursion and so it is another subject that i'm having difficulty with... i don't understand why the output is what it is... what mostly i'm stuck with is the line: result = k + tri_recursion(k - 1) if k runs 6 times, beginning with 6 - and so it is: 6 + (6-1) (should equal 11...but no 11 in output) i'm surely missing something here, but i don't know what it is.. RE: num+1 - Larz60+ - Oct-17-2022 the function is recursive, so result doesn't get displayed until all iterations are complete (because the next iteration is called if k is greater than zero). Add a print statement to show value of k on each iteration, and this becomes clear: def tri_recursion(k): if(k > 0): print(f"k: {k}") result = k + tri_recursion(k - 1) print(result) else: result = 0 return result print("\n\nRecursion Example Results") tri_recursion(6)
|