1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Taking n number of integer inputs from a single line in python

Discussão em 'Python' iniciado por Stack, Outubro 25, 2024 às 12:32.

  1. Stack

    Stack Membro Participativo

    So basically I take an n value with n = int(input("number of boats")) from user, then taking n amount of integer inputs from a single line (let's say my input is 2 6 3 6 4 7 4 and they wrote 2, I'd only take the first two numbers 2 6) and appending that into a list (which I define before as mylist = []). I want to have them as integers and not a string in my list. How can I do this?


    EDIT:

    Okay perhaps my wording wasn't the best, so I'll try explaining differently. I'm taking an input from a .txt file and the file has:

    3 23 56 36 48 46 97

    The 3 at the start determines how many boats there are, and the 23 56 for example are values for the first boat. I want to take input that determines how many boats, then take all the values as input and put them all into one list [23, 56, 36, 48, 46, 97]. Note that I have to use input and not file reading because there will be different values tested. I need the values as integers so I can't take every line as a string.

    Continue reading...

Compartilhe esta Página