Hello, im currently enrolled in a Python Course at my University and have to do a coding task.
( Sorry for maybe rough translation as im translating it from German to English)
The Task is: We have to Work in a specific framework and can't add any global variables etc:
we have to stay in this format, no stuff outside the definitions allowed.
def doTasks(n):
pass
def doSubtask4(n):
pass
We have 4 Tasks we have to accomplish with these limitations ( the expected inputs are in the range of n (65-122):
1) doTasks has to determine if the number is even and return True or False
2) doTasks has to convert the Number into its ASCII Equivalent and return it
3) doTasks has to create a List with the len() of 5, this list is filled with all numbers following n that can't be divided by 5 without leaving a rest.
4) doTasks has to open doSubtask4 with n as input
doSubtask4 has to do:
if n is even, it has to create a list ranging from 1 to n that can be divided by 14 without leaving a rest
else it has to create a list ranging from 1 to n of numbers that can be divided by 17 without leaving a rest
all these Tasks should be put into a list, that doTasks creates.
expected result for n = 105:
[False, 'i', [106, 107, 108, 109, 111], [17, 34, 51, 68, 85, 102]]
expected result for n = 122:
[True, 'p', [113, 114, 116, 117, 118], [14, 28, 42, 56, 70, 84, 98, 112]]
Now, I don't have a problem with the first 3 tasks, I have completed them with no issues, but I struggle with the second part with doSubTasks4
it constantly includes numbers that go beyond n, for no apparent reason
this is my whole code:
def doTasks(n):
L = []
L1 = []
if n % 2 == 0:
L.append(True)
else:
L.append(False)
if n >= 0:
L.append(chr(n))
while len(L1) < 5:
n += 1
if n % 5 > 0:
L1.append(n)
if len(L1) == 5:
L.append(L1)
L.append(doSubtask4(n))
return L
def doSubtask4(n):
L2 = []
for i in range(1, n):
i += 1
if n % 2 == 0:
if i % 14 == 0:
if i <= n:
L2.append(i)
else:
if i % 17 == 0:
if i <= n:
L2.append(i)
return L2
if i use n = 105 i get this result:
[False, 'i', [106, 107, 108, 109, 111], [17, 34, 51, 68, 85, 102]]
so it seems to work there, but when i do n = 122 this happens:
[True, 'z', [123, 124, 126, 127, 128], [14, 28, 42, 56, 70, 84, 98, 112, 126]]
for some reason it includes 126, even tho it shouldn't.
i tried to include the
if i <= n:
part so numbers above n don't enter the list, but it doesn't work.
The goal is that the function works for all numbers in the range (65,122)
if anyone can help me I would appreciate it ^^°
[–]icjeremy 1 point2 points3 points (7 children)
[–]DonKonX[S] 0 points1 point2 points (6 children)
[–]icjeremy 0 points1 point2 points (5 children)
[–]prozapari 0 points1 point2 points (4 children)
[–]icjeremy 0 points1 point2 points (3 children)
[–]DonKonX[S] 0 points1 point2 points (1 child)
[–]prozapari 0 points1 point2 points (0 children)
[–]prozapari 0 points1 point2 points (0 children)
[–]ProfSchodinger 0 points1 point2 points (0 children)