简体   繁体   中英

Taking in integer input separated by space (invalid literal error) Python

So the goal of this code is to remove duplicates from the input and then print out a list without the duplicates and I think I got it but I can't seem to remember how to take in input with spaces and none of the things I have looked up so far have been very helpful to my case. Here's my code.

def eliminateDuplicates(lst):
    strnumbers = str(lst)
    listnumbers = list(strnumbers.split())    
    newlist = []
    for number in listnumbers:
        if number not in newlist:
            newlist.append(number)
    return newlist

def main():
    numbers = int(input("Enter numbers separated by space"))
    print("The distinct numbers are: ", eliminateDuplicates(numbers)) 

main()

you cannot do

int("1 2 3")

but you can do

[int(x) for x in "1 2 3".split()]

one of the errors is coming from your int(input()) ... however I think wim is right

strnumbers = str(lst)
listnumbers = list(strnumbers.split())  

I think this sillyness is the cause of your problems ( , and [ characters are going into your numbers). Just iterate over the input lst .

You will also need to work at sending a proper list into your function, which means you will need to change this line:

numbers = int(input("Enter numbers separated by space"))

I will leave that bit up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM