简体   繁体   中英

python last number repeat more than once

My problem is that i dont get the desired output which i want. The list numbers can change as well as the size of the list.

Task: Complete the method so that it will search through the list passed as a parameter to see if the last value in the list occurs more than once. If the last value occurs more than once, return True (don't print "True"). Otherwise, return False (don't print "False"). If the list is empty, return False. Call your method from the main method, send it a list, and print the result.

def last_repeats(list):
  last_number = list[-1]
  lenght = len(list)
  for x in range (0,lenght-1):
    if list[x] == last_number:
      return True
    else:
      return False
def main():
  list = [0,0,5,5]
  print(last_repeats(list))


############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
  main()

Output I need:

True

Output i get:

False

You are returning after checking the first one, even though it might be false. Consider your array [0, 0, 5, 5]. the first element 0 != 5, so it immediately returns False.

To fix that, return False after the function ends

def last_repeats(lis): # 'list' is a keyword in python
  if len(lis) == 0: return False # Task requirement
  last_number = lis[-1]
  length = len(list) # typo?
  for x in range(length): # range(0, n) == range(n)
    if lis[x] == last_number:
      return True
  return False # So now you have checked every element in the list
def main():
  lis = [0, 0, 5, 5]
  print(last_repeats(lis))


############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
  main()

Also I just read that if the list is empty, it should return False. Currently, the code gives "IndexError". Try to fix it yourself!

Have fun!

The range of values for range(start, stop, \\[step\\]) is determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop . So for x in range(0, lenght - 1) will go until the second to last item. It should be range(0, lenght) :

def last_repeats(list):
  last_number = list[-1]
  lenght = len(list)
  vreturn = False
  for x in range (0,lenght):
    if list[x] == last_number:
      vreturn = True
  return vreturn

You get the False output because your if statement returns False on the first appearance of a number not equal to value of last_number (ie the first element in list) and that's because of the 'else' block which is not needed here.

You should change your if statement to just return True inside the loop. False returned outside the loop:

for x in range (lenght):
    if list[x] == last_number:
      return True
    return False
def last_repeats(lis): # 'list' is a keyword in python
  last_number = lis[-1]
  length = len(list) # typo?
  for x in range(length): # range(0, n) == range(n)
    if lis[x] == last_number:
      return True
  return False # So now you have checked every element in the list
def main():
  lis = [0, 0, 5, 5]
  print(last_repeats(lis))


############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
  main()

you can use:

def last_repeats(my_list):
    return my_list and my_list[-1] in my_list[:-1]

def main():
    my_list = [0, 0, 5, 5]
    print(last_repeats(my_list))

############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
    main()

output:

True

You need to put the return false statement outside the for loop.

def last_repeats(list):
  last_number = list[-1]
  lenght = len(list)
  for x in range (0,lenght-1):
    if list[x] == last_number:
      return True
  return False
def main():
  list = [0,0,5,5]
  print(last_repeats(list))


############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
  main()

Also you do not need the else case. This edit should achieve the purpose you are trying. At present your function returns false as soon as it inquires a non matching element. Which should be only returned false after you have checked uptill second last element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM