How to read limited number of input separated by space in Python?

后端 未结 2 874
南旧
南旧 2021-01-17 07:00

While coding for competitions in codechef, I face this problem with python, to read

3     # number of required input  
1 4 2 # inputs 
         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-17 07:00

    To separate input by spaces:

    data=list(map(int,input().split(' ')))
    

    To verify wether the correct amount of numbers were passed:

    if len(data) > n: # where n is your number of required input
        # Raise an exception or ask for the input again.
    

提交回复
热议问题