The Student Room Group

Computer Science Python

-----------------------------------------------------------
originalList = [194, 846, 39, 568, 832, 498, 141, 423, 908, 189]

# -----> Write your code here

listBefore = []

# ------------------------------------------------------------
# Subprograms
# ------------------------------------------------------------
def findBefore (pList, pTarget):

# -----> Write your code here

found = False
for i in range(0, len(pList)):
if pList != pTarget:
listBefore.append(pList)
else:
found = True
return(listBefore)
if found == False:
return([])




# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------

# -----> Write your code here

target = input("Please enter a target number: ")

if findBefore(originalList, target) == []:
print("That number is not in the list")
else:
print(listBefore)


Hi, can someone let me know why this code doesn't work? Im trying to print out every element in the list before the target value but it just always comes up with That number is not In the list. Thank you!
Reply 1
Original post by lkrnix
-----------------------------------------------------------
originalList = [194, 846, 39, 568, 832, 498, 141, 423, 908, 189]

# -----> Write your code here

listBefore = []

# ------------------------------------------------------------
# Subprograms
# ------------------------------------------------------------
def findBefore (pList, pTarget):

# -----> Write your code here

found = False
for i in range(0, len(pList)):
if pList != pTarget:
listBefore.append(pList)
else:
found = True
return(listBefore)
if found == False:
return([])




# ------------------------------------------------------------
# Main program
# ------------------------------------------------------------

# -----> Write your code here

target = input("Please enter a target number: ")

if findBefore(originalList, target) == []:
print("That number is not in the list")
else:
print(listBefore)


Hi, can someone let me know why this code doesn't work? Im trying to print out every element in the list before the target value but it just always comes up with That number is not In the list. Thank you!

Your list contains numbers, but the input function returns a string, so you're passing a string to your findBefore function! Hopefully you can see what minor modification you need to make to get it working :smile:
Ahh thank you so much!!
Reply 3
Original post by lkrnix
Ahh thank you so much!!

no problem - I wish they were all that easy to spot!

Quick Reply

Latest

Trending

Trending