1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to check if an input is a specific string when putting into a list

Discussão em 'Python' iniciado por Stack, Setembro 15, 2024.

  1. Stack

    Stack Membro Participativo

    I'm very new to coding. I'm trying to have the user input a number, that input is checked to see if it says "stop", then the number is added to a list. I want it to keep going until you type "stop" and then display the list. I've tried a bunch of different ways and I feel like I'm getting no where.

    def listsample():
    sample = []
    killcode = str("stop")
    sample.append(input("enter number"))
    while sample[-1] != killcode:
    if killcode not in sample:
    sample.append(input("enter number"))
    elif killcode in sample:
    return sample


    def printsample(sample):
    print(sample)


    listsample()
    printsample(listsample())


    I've tried moving stuff around a lot. I didn't keep my previous trial and error samples. I understand the bare minimum of loops and stuff, this is kind of me trying to figure them out.

    Continue reading...

Compartilhe esta Página