[Python] Can't remove the last item in list, [-1] isnt working?

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    Line 30: 'new_2.remove(new_2[-1])' this is meant to remove the last letter in the list (which will be the 's' at the end, because I want to remove the 's' when the number before it is '1', because, y'know, grammar. But it removes the first 's' even though I'm having it remove the last item by doing [-1]. Everything else is working but I dont know why it's removing the last 's' and not the first one?!

    import random
    import time
    will = ['No', 'Yes', 'Maybe', 'Probably not', 'Probably', 'Definitely not', 'Definitely']

    time_1 = ['1']
    time_2 = ['days', 'seconds', 'years', 'weeks', 'minutes', 'months']

    def listToString(str_in):
    str1 = ""
    for x in str_in:
    str1 += x
    return str1


    print("Enter a question starting\nwith 'when' or 'will' to shake 8 ball\n"
    '0 to exit\n')

    while True:
    kb_in = input(': ').lower().strip()
    print('\nShaking...\n')
    time.sleep(0.6)
    if kb_in == '0':
    print('bye'); exit()
    elif 'when' in kb_in:
    new = random.choice(time_1)
    if new == '1':
    print(new, end = ' ')
    new_1 = random.choice(time_2)
    new_2 = list(new_1)
    new_2.remove(new_2[-1]) # not taking last 's' out of 'seconds' ??
    print(listToString(new_2), '\n')
    else:
    print(f'{new} {random.choice(time_2)}\n')
    elif 'will' in kb_in and 'when' not in kb_in:
    print(random.choice(will))
    else:
    print("\nInput does not contain 'will' or 'when'\n")

    Continue reading...

Compartilhe esta Página