Get a list of numbers as input from the user

前端 未结 17 1168
生来不讨喜
生来不讨喜 2020-11-21 22:26

I tried to use raw_input() to get a list of numbers, however with the code

numbers = raw_input()
print len(numbers)

the input

17条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 23:21

    try this one ,

    n=int(raw_input("Enter length of the list"))
    l1=[]
    for i in range(n):
        a=raw_input()
        if(a.isdigit()):
            l1.insert(i,float(a)) #statement1
        else:
            l1.insert(i,a)        #statement2
    

    If the element of the list is just a number the statement 1 will get executed and if it is a string then statement 2 will be executed. In the end you will have an list l1 as you needed.

提交回复
热议问题