accepting multiple user inputs separated by a space in python and append them to a list

后端 未结 4 959
离开以前
离开以前 2020-11-30 11:45

How can I accept multiple user inputs separated by a space? I don\'t know the number of inputs, but I do know they are all ints.

Here\'s some example inputs:

相关标签:
4条回答
  • 2020-11-30 12:06

    Try this:

    nums = [int(i) for i in raw_input("Enter space separated inputs: ").split()]
    
    0 讨论(0)
  • 2020-11-30 12:28
    s = raw_input("Please enter your numbers: ")
    
    mynums = [int(i) for i in s.split()]
    # OR
    mynums = map(int, s.split())
    
    0 讨论(0)
  • 2020-11-30 12:29

    for python 2.x

    x,y = map(int,raw_input().split())

    it take two variable x and y of int type separated by space and you can replace int with your desired type

    for python 3.x

    x,y = input().split()

    it takes two variables x and y of string type separated by space and you have to convert explicitly

    0 讨论(0)
  • 2020-11-30 12:29

    x,y=map(int,input().split()) #this will take input separated by space and map #into int

    0 讨论(0)
提交回复
热议问题